Search code examples
phpwordpresswoocommerceproductcustom-taxonomy

Display product attributes on specific Woocommerce product category archives page


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.

screenshot

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>'; 
}

Solution

  • 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 = wc_attribute_label($taxonomy);
                    // 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.

    enter image description here


    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