2016-08-04 74 views
1

我試圖爲WooCommerce中的不同類別顯示不同的自定義字段。爲WooCommerce中的不同類別顯示不同的自定義字段

我用下面的條件語句中的內容,單product.php模板文件:

 if(is_product_category('categoryname')) 
    { 
     // display my customized field 
    } 
else 
{ 
do_action('woocommerce_after_single_product_summary'); 
} 

但是這不是爲我工作。

有沒有更好的方法來糾正這個問題?

謝謝。

回答

1

在單個產品模板中,條件is_product_category()不適用於您。正確的條件是兩個組合在這種情況下:

if (is_product() && has_term('categoryname', 'product_cat')) { 

    // display my customized field 

} 
.... 

它看起來像你試圖重寫content-single-product.php模板。

移動woocommerce_single_product_summary鉤子上ELSE語句中不是一個好主意,只要你不想顯示'categoryname'產品有3上鉤功能:

* @hooked woocommerce_output_product_data_tabs - 10 
* @hooked woocommerce_upsell_display - 15 
* @hooked woocommerce_output_related_products - 20 

相反(覆蓋的模板在這裏)你可以嵌入你的代碼(在你的活動兒童主題或主題的function.php文件中)使用更方便的2個鉤子:

//In hook 'woocommerce_single_product_summary' with priority up to 50. 

add_action('woocommerce_single_product_summary', 'displaying_my_customized_field', 100); 
function displaying_my_customized_field($woocommerce_template_single_title, $int) { 
    if (is_product() && has_term('categoryname', 'product_cat')) { 

     // echoing my customized field 

    } 
}; 

OR

// In hook 'woocommerce_after_single_product_summary' with priority less than 10 

add_action('woocommerce_after_single_product_summary', 'displaying_my_customized_field', 5); 
function displaying_my_customized_field($woocommerce_template_single_title, $int) { 
    if (is_product() && has_term('categoryname', 'product_cat')) { 

     // echoing my customized field 

    } 
}; 
+0

非常感謝你。覆蓋content-single-product.php爲我工作。再次感謝。 –

相關問題