2
votes

I am developing cart website using woocommerce plugin in wordpress.In products i have two attributes as like SIZE and COLOR.

Currently Product name and Price is displaying on product listing page dafault.now i want to add SIZE attributes below price in product listing page.

I know about to add hook but i don't know that how to add SIZE using hook?

1
Did you try with: $variations = $product->get_available_variations();?dingo_d
@dingo_d no. i didn't try that. i don't know how to add that hook in woocommerceBhavesh Patel
I'm working on it, should be pretty easy.dingo_d
can you guide me. how should i implement that? @dingo_dBhavesh Patel
Check the edited answer.dingo_d

1 Answers

2
votes

EDITED:

I misunderstood you. This should do it (checked it on my woocommerce test page):

if (!function_exists('shop_attributes_in_loop')) {
    function shop_attributes_in_loop(){
        global $product;
            $attributes = $product->get_attributes();
            if(!empty($attributes)){
                $attribute_single = array_keys($attributes);
                $myArray = array();
            echo '<div class="product_attributes">';
                foreach ($attribute_single as $attribute => $value) {
                    $myArray[] = ucfirst($value);
                }
            echo implode(', ', $myArray).'</div>';
            }
    }
}


add_action('woocommerce_after_shop_loop_item', 'shop_attributes_in_loop');

This will put your product attributes, if existing, in a div .product_attrributes. You can easily add a translatable string saying that those are product attributes like

echo esc_html('Product attributes', 'my-theme-name') .'<div class="product_attributes">'

in the first echo.