Display product attributes on specific Woocommerce product category archives page

无人久伴 提交于 2019-12-24 05:19:10

问题


I want to show two attributes on the category pages, with the attribute name and value only on specific categories.

This code that I found displays the labels of the attributes, but is duplicating the value and I am really struggling with a show if categories variable. Any help is greatly appreciated.

The code:

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

    $product_attributes = array( 'pa_set', 'pa_team');
    $attr_output = array();

    // Loop through the array of product attributes
    foreach( $product_attributes as $taxonomy ){
        if( taxonomy_exists($taxonomy) ){
            $label_name = get_taxonomy( $taxonomy )->labels->singular_name;
            $value = $product->get_attribute('pa_set');

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

    // Output attribute name / value pairs separate by a "<br>"
    echo '<div class="product-attributes">'.implode( '<br>', $attr_output 
    ).'</div>'; 
}

回答1:


Updated - The problem comes from the following line, where the product attribute attribute value is always for the same product attribute:

$value = $product->get_attribute( 'pa_set' );

and it should be this instead:

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

The complete revisited code will be:

add_action('woocommerce_after_shop_loop_item','display_loop_product_attribute' );
function display_loop_product_attribute() {
    global $product;

    $product_attributes = array('pa_set', 'pa_team'); // Defined product attribute taxonomies.
    $attr_output = array(); // Initializing

    // Loop through the array of product attributes
    foreach( $product_attributes as $taxonomy ){
        if( taxonomy_exists($taxonomy) ){
            if( $value = $product->get_attribute($taxonomy) ){
            // The product attribute label name
            $label_name = get_taxonomy( $taxonomy )->labels->singular_name;
                // Storing attributes for output
                $attr_output[] = '<span class="'.$taxonomy.'">'.$label_name.': '.$value.'</span>';
            }
        }
    }
    // Output attribute name / value pairs separate by a "<br>"
    echo '<div class="product-attributes">'.implode('<br>', $attr_output).'</div>';
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.


Targeting Product category archive pages:

You will use the conditional tag is_product_category() inside the function on an IF statement…

For specific product category archive pages, you can set them as explained here inside the function in an array, like:

if( is_product_category( array('chairs', 'beds') ) {
    // Here go the code to be displayed
}

You will just need to set the right product categories slugs in the array…


Related: Show WooCommerce product attributes in custom home and product category archives



来源:https://stackoverflow.com/questions/53510707/display-product-attributes-on-specific-woocommerce-product-category-archives-pag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!