Display specific product attribute values on archives category pages

与世无争的帅哥 提交于 2019-12-14 02:11:55

问题


I want to show specific product attributes on category page in WooCommerce. Attributes would be shown after the product title, before the short description.

The attributes are pa_nopeus, pa_liito, pa_vakaus and pa_feidi. They are only numerical values. I just want to show the values and not the names, pretty much like this:

Product Name
4 / 4 / 1 / 2
Short description

If these values do not exist in the product, line would not be shown at all.

I want to add this to the (template) code and not by using a plugin. I believe it will be added to content-product.php.

#content-product.php start.... 

do_action( 'woocommerce_shop_loop_item_title' );

VALUES HERE SOMEHOW?


/**
 * woocommerce_after_shop_loop_item_title hook.
 *
 * @hooked woocommerce_template_loop_rating - 5
 * @hooked woocommerce_template_loop_price - 10
 */
do_action( 'woocommerce_after_shop_loop_item_title' );

#rest of the content-product.php...

How can I achieve this?

Thanks


回答1:


The best way to do what you expect (without overriding the templates) is to use a function that we hook in 'woocommerce_shop_loop_item_title' hook.

In this case the code below goes on function.php file of your active child theme (or theme).

Here is the code:

// For WooCommerce below version 3.0
add_action( 'woocommerce_shop_loop_item_title', 'custom_attributes_display', 20 );
function custom_attributes_display(){

    // Just for product category archives pages
    if(is_product_category()){
        global $product;

        // the array of attributes names
        $attribute_names = array('pa_nopeus', 'pa_liito', 'pa_vakaus', 'pa_feidi');
        foreach( $attribute_names as $key => $attribute_name ) {

            // Getting the value of an attribute
            $attribute_value = array_shift(wc_get_product_terms( $product->id, $attribute_name));

            // Displays only if attribute exist for the product
            if(!empty($attribute_value) || $attribute_value == '0' ){ // Updated
                echo $attribute_value;

                // Separating each number by a " / "
                if($key < 3) echo ' / ';
            }
        }
    }
}

For woocommerce 3.0+ see: Add Attributes to Short Description in WooCommerce 3.0+

So you will get just after the title, on product category archives pages what you are expecting.


Or alternatively you can use the code above in the template content-product.php, this way:

#content-product.php start.... 

do_action( 'woocommerce_shop_loop_item_title' );

/////////////// HERE IS THE CODE ////////////////

// Just for product category archives pages
if(is_product_category()){
    global $product;

    // the array of attributes names
    $attribute_names = array('pa_nopeus', 'pa_liito', 'pa_vakaus', 'pa_feidi');
    foreach( $attribute_names as $key => $attribute_name ) {

        // Getting the value of an attribute
        $attribute_value = array_shift(wc_get_product_terms( $product->id, $attribute_name));

        // Displays only if attribute exist for the product
        if(!empty($attribute_value) || $attribute_value == '0' ){
            echo $attribute_value;

            // Separating each number by a " / "
            if($key < 3) echo ' / ';
        }
    }
}


/**
 * woocommerce_after_shop_loop_item_title hook.
 *
 * @hooked woocommerce_template_loop_rating - 5
 * @hooked woocommerce_template_loop_price - 10
 */
do_action( 'woocommerce_after_shop_loop_item_title' );

#rest of the content-product.php...

But the first solution is more recommended and elegant.

The code is tested and works



来源:https://stackoverflow.com/questions/39580489/display-specific-product-attribute-values-on-archives-category-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!