为WooCommerce中的不同类别显示不同的自定义字段
问题描述:
我试图为WooCommerce中的不同类别显示不同的自定义字段。为WooCommerce中的不同类别显示不同的自定义字段
我用下面的条件语句中的内容,单product.php模板文件:
if(is_product_category('categoryname'))
{
// display my customized field
}
else
{
do_action('woocommerce_after_single_product_summary');
}
但是这不是为我工作。
有没有更好的方法来纠正这个问题?
谢谢。
答
在单个产品模板中,条件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
}
};
非常感谢你。覆盖content-single-product.php为我工作。再次感谢。 –