如果产品属于特定类别,如何设置标签(仅) [英] How can I set tab (only) if the product is in a certain category WooCommerce
问题描述
我有一个标签集,可以添加一个包含WooCommerce中规范的标签。我想将其包装到if语句中,以便仅在产品属于特定类别的情况下才设置选项卡。
I have a tab set to add a tab which contains specifications in WooCommerce. I'd like to wrap it into an if statement to only set the tab if the product is part of a certain category.
add_filter( 'woocommerce_product_tabs', 'woo_custom_product_tabs' );
function woo_custom_product_tabs( $tabs ) {
global $post;
if ($product->is_category("Mobile Phones")) {
$tabs['custom_specification'] = array( 'title' => __( 'Specification', 'woocommerce' ), 'priority' => 50, 'callback' => 'woo_custom_specification_content' );
}
}
检查WooCommerce类别的正确代码是什么
What is the right code to check the category for WooCommerce in the if statements brackets?
推荐答案
条件 is_category()
将如果您在类别存档页面上,则返回true。
The conditional is_category()
will return true if you are on a category archive page.
由于您需要单个产品页面的条件,因此可以使用 is_product()
有条件地以这种方式组合:
As you need a conditional for single product pages, you will target single product pages with is_product()
conditional combined this way:
if ( is_product() && has_term( 'Mobile Phones', 'product_cat' ) ) {
$tabs['custom_specification'] = array( 'title' => __( 'Specification', 'woocommerce' ), 'priority' => 50, 'callback' => 'woo_custom_specification_content' );
}
或者也可以尝试以下一种方法:
Or you could try also, in case, this one too:
if( is_product() && has_category( 'Mobile Phones' ) ) {
$tabs['custom_specification'] = array( 'title' => __( 'Specification', 'woocommerce' ), 'priority' => 50, 'callback' => 'woo_custom_specification_content' );
}
@edit ::您错过了返回$ tabs;
的功能结尾处的最后一个括号 }
。
@edit: You've missed return $tabs;
at the end of your function before last closing bracket }
.
参考文献:
- Remove product content based on category
- How to check if a woocommerce product has any category assigned to it?
这篇关于如果产品属于特定类别,如何设置标签(仅)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!