在档案类别页面上显示特定的产品属性值

Display specific product attribute values on archives category pages

我想在 WooCommerce 的类别页面上显示特定的产品属性。属性将显示在产品标题之后,简短描述之前。

属性为pa_nopeus,pa_liito,pa_vakaus pa_feidi。它们只是数值。我只想显示值而不是名称,就像这样:

Product Name
4 / 4 / 1 / 2
Short description

如果产品中不存在这些值,则根本不会显示行。

我想将其添加到(模板)代码中,而不是使用插件。我相信它会被添加到 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...

我怎样才能做到这一点?

谢谢

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.

在这种情况下,下面的代码继续 function.php 您的活动 child 主题(或主题)的文件。

代码如下:

// 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+

因此,您将在产品类别档案页面上的标题之后获得您所期望的内容。


或者您可以在模板 content-product.php 中使用上面的代码,这样:

#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...

但是第一种方案更推荐也更优雅

代码已经过测试并且有效