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

问题描述:

我想在WooCommerce的分类页面上显示特定的产品属性。在简短说明之前,产品标题之后会显示属性。在档案分类页面上显示特定的产品属性值

的属性为pa_nopeuspa_liitopa_vakauspa_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... 

我怎样才能做到这一点?

感谢

做你所期望的(不重写模板)的最好方法是使用我们'woocommerce_shop_loop_item_title'吊钩的功能。

在这种情况下,下面的代码继承你的活动子主题(或主题)的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+看到: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... 

但第一个解决方案是比较推荐的优雅。

的代码测试和工程

+0

感谢您这两种。我明白这是如何工作的。测试这两个并以相同的方式工作。我会用'functions.php'去。修改。 只有没有工作的东西,它不显示值,如果它是0(它仍然是值,而不是“空”)。代码应该有点不同? –

+0

@JuhaKytö好吧我现在编辑我的代码,对于那种情况...好吧改变了一点这种情况尝试它,并告诉我它是否适用于“0”值 – LoicTheAztec

+0

是的,现在它完美的工作!非常感谢你! –