• Resolved piotrk87

    (@piotrk87)


    Hi Team,

    I am looking for a way to list some of product attributes on the main page of the shop. I’ve found the following code, however there is a mistake there which causes all the attributes to show the same value.

    Is there any way to have the correct value shown for each attribute? Or maybe a plugin or other solution will work?

    The code I was talking about is below:

    add_action('woocommerce_after_shop_loop_item','add_attribute');
    function add_attribute() {
        global $product;
    
        $product_attributes = array( 'pa_weight', 'pa_quantity', 'pa_length', 'pa_color' );
        $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_weight');
    
                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>';
    }
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Product attibutes on main shop page’ is closed to new replies.