Wordpress Woocommerce Показать атрибуты на странице магазина - PullRequest
1 голос
/ 30 мая 2020

Я хочу добавить некоторые атрибуты на страницу магазина WordPress. Этот код, который я нашел в Stackoverflow, показывает все метки атрибутов, но с одинаковыми именами атрибутов.

add_action('woocommerce_after_shop_loop_item_title','add_attribute');
function add_attribute() {
global $product;

$product_attributes = array( 'pa_country','pa_class','pa_faction','pa_gender' );
$attr_output = array();


foreach( $product_attributes as $taxonomy ){
    if( taxonomy_exists($taxonomy) ){
        $label_name = get_taxonomy( $taxonomy )->labels->singular_name;
        $value = $product->get_attribute('pa_country','pa_class','pa_faction','pa_gender');

        if( ! empty($value) ){

            $attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$value.'</span>';
        }
    }}


echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';

}

текущее состояние

Мне просто нужна небольшая помощь, чтобы он показал все правильные атрибуты.

Ответы [ 2 ]

0 голосов
/ 30 мая 2020

В вашем коде есть небольшие ошибки. Вместо этого попробуйте следующее:

add_action('woocommerce_after_shop_loop_item_title', 'display_shop_loop_product_attributes');
function display_shop_loop_product_attributes() {
    global $product;

    // Define you product attribute taxonomies in the array
    $product_attribute_taxonomies = array( 'pa_country', 'pa_class', 'pa_faction', 'pa_gender' );
    $attr_output = array(); // Initializing

    // Loop through your defined product attribute taxonomies
    foreach( $product_attribute_taxonomies as $taxonomy ){
        if( taxonomy_exists($taxonomy) ){
            $label_name = wc_attribute_label( $taxonomy, $product );

            $term_names = $product->get_attribute( $taxonomy );

            if( ! empty($term_names) ){
                $attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$term_names.'</span>';
            }
        }
    }

    // Output
    echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';
}

Код входит в functions. php файл вашей активной дочерней темы (или активной темы). проверено и работает.


Только для простых продуктов вы будете использовать вместо этого следующее:

add_action('woocommerce_after_shop_loop_item_title', 'display_shop_loop_product_attributes');
function display_shop_loop_product_attributes() {
    global $product;

    // Only for simple products
    if ( ! $product->is_type( 'simple' ) ) return;

    // Define you product attribute taxonomies in the array
    $product_attribute_taxonomies = array( 'pa_country', 'pa_class', 'pa_faction', 'pa_gender' );
    $attr_output = array(); // Initializing

    // Loop through your defined product attribute taxonomies
    foreach( $product_attribute_taxonomies as $taxonomy ){
        if( taxonomy_exists($taxonomy) ){
            $label_name = wc_attribute_label( $taxonomy, $product );

            $term_names = $product->get_attribute( $taxonomy );

            if( ! empty($term_names) ){
                $attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$term_names.'</span>';
            }
        }
    }

    // Output
    echo '<div class="product-attributes">'.implode( '<br>', $attr_output ).'</div>';
}

Код входит в functions. php файл вашей активной дочерней темы (или активной тема). проверено и работает.

0 голосов
/ 30 мая 2020

Я решил проблему. это некрасиво, но работает.

add_action( 'woocommerce_after_shop_loop_item_title', 'display_size_attribute', 5 );
function display_size_attribute() {
global $product;
if ( $product->is_type('simple') ) {
    $taxonomy = 'pa_country';
    echo '<span class="attribute-s">Country: ' . $product->get_attribute($taxonomy) . '</span>','<br>';
}
 if ( $product->is_type('simple') ) {
    $taxonomy = 'pa_class';
    echo '<span class="attribute-s">Class: ' . $product->get_attribute($taxonomy) . '</span>','<br>';
}
 if ( $product->is_type('simple') ) {
    $taxonomy = 'pa_faction';
    echo '<span class="attribute-s">Faction: ' . $product->get_attribute($taxonomy) . '</span>','<br>';
}
 if ( $product->is_type('simple') ) {
    $taxonomy = 'pa_gender';
    echo '<span class="attribute-s">Gender: ' . $product->get_attribute($taxonomy) . '</span>','<br>';
}}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...