AltaiR-05
@AltaiR-05

Как вывести атрибуты товара woocommerce в списке товаров?

Доброе утро. Подскажите как вывести атрибуты товаров в таком виде:
a24b5e27279c4c22b090f0e3ee0b3ca7.png
Я добавил вариативный товар. В самом товаре добавил атрибут с названием Количество и со значениями:
66bba019517347499b6f213c294160b3.png
И в каждом значении добавил свою цену:
5bde13a3102248ad921f25782ac205de.png
а в файл functions.php добавил следующий код:
add_action( 'woocommerce_after_shop_loop_item_title', 'show_attributes', 20 );
 
function show_attributes() {
  global $product;
  $product->list_attributes();
}

И он у меня вывел только значения атрибутов (без цен) и в одну строку, а хотелось бы как у меня в psd в разных ячейках и с ценами:
c5854894324349c39b07e841f8fa2567.png
  • Вопрос задан
  • 4011 просмотров
Решения вопроса 1
Palych_tw
@Palych_tw
Типа веб-разработчик
Попробуйте так.

add_action( 'woocommerce_after_shop_loop_item_title', 'show_attributes', 20 );
 
function show_attributes() {
global $product;
if ($product->product_type == 'variable') {
        $attributes = $product->get_attributes();?>
        <ul>
            <?php  $variations_ids = $product->get_children();
            foreach ($variations_ids as $variation_id) {
                $variation = $product->get_child($variation_id);
                $variation_data = $variation->get_variation_attributes();
                $attributes     = $variation->parent->get_attributes();
                $description    = array();
                $attr_name        = '';

                if ( is_array( $variation_data ) ) {

                    foreach ( $attributes as $attribute ) {

                        if ( ! $attribute[ 'is_variation' ] ) {
                            continue;
                        }

                        $variation_selected_value = isset( $variation_data[ 'attribute_' . sanitize_title( $attribute[ 'name' ] ) ] ) ? $variation_data[ 'attribute_' . sanitize_title( $attribute[ 'name' ] ) ] : '';
                        $description_name         = esc_html( wc_attribute_label( $attribute[ 'name' ] ) );
                        $description_value        = __( 'Any', 'woocommerce' );

                        if ( $attribute[ 'is_taxonomy' ] ) {

                            $post_terms = wp_get_post_terms( $variation->id, $attribute[ 'name' ] );

                            foreach ( $post_terms as $term ) {
                                if ( $variation_selected_value === $term->slug ) {
                                    $description_value = esc_html( apply_filters( 'woocommerce_variation_option_name', $term->name ) );
                                }
                            }

                        } else {

                            $options = wc_get_text_attributes( $attribute[ 'value' ] );

                            foreach ( $options as $option ) {

                                if ( sanitize_title( $variation_selected_value ) === $variation_selected_value ) {
                                    if ( $variation_selected_value !== sanitize_title( $option ) ) {
                                        continue;
                                    }
                                } else {
                                    if ( $variation_selected_value !== $option ) {
                                        continue;
                                    }
                                }

                                $description_value = esc_html( apply_filters( 'woocommerce_variation_option_name', $option ) );
                            }
                        }

                        $description[] =  rawurldecode( $description_value );

                    }
                    $attr_name .= implode( '', $description );

                } ?>
                <li><?php echo $attr_name;?> - <?php echo $variation->get_price_html();?></li>
           <?php } ?>
        </ul>
    <?php }
}
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы