在归档类别页面上显示特定的产品属性值 [英] Display specific product attribute values on archives category pages
问题描述
我想在WooCommerce的类别页面上显示特定的产品属性.属性将显示在产品标题之后,简短描述之前.
I want to show specific product attributes on category page in WooCommerce. Attributes would be shown after the product title, before the short description.
属性为 pa_nopeus
, pa_liito
, pa_vakaus
和 pa_feidi
.它们只是数值.我只想显示值而不是名称,就像这样:
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.
我想将此添加到(模板)代码中,而不是使用插件.我相信它将被添加到 content-product.php.
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...
我该如何实现?
谢谢
推荐答案
做您期望的最好方法(不覆盖模板)是使用我们在
'woocommerce_shop_loop_item_title'
挂钩中挂钩的函数.
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文件中.
这是代码:
// 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 ' / ';
}
}
}
}
对于woocommerce 3.0+,请参见:在WooCommerce 3.0+中的简短说明中添加属性
因此,您将在标题之后的产品类别档案页面上获得所需的内容.
So you will get just after the title, on product category archives pages what you are expecting.
或者,您也可以通过以下方式在模板 content-product.php
中使用以上代码:
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.
该代码已经过测试并且可以正常工作
这篇关于在归档类别页面上显示特定的产品属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!