Bhavesh Patel
Bhavesh Patel

Reputation: 21

How to add product attributes in woocommerce product lisitng page in wordpress?

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?

Upvotes: 2

Views: 3871

Answers (1)

dingo_d
dingo_d

Reputation: 11670

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.

Upvotes: 2

Related Questions