简体   繁体   中英

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM