我正在使用来自这个答案的代码:
I am using the code coming from this answer:
仅隐藏标签对于 WooCommerce 单个产品页面中的某些产品
代码如下:
add_filter( 'woocommerce_product_tabs', 'conditionaly_removing_product_tabs', 98 );
function conditionaly_removing_product_tabs( $tabs ) {
// Get the global product object
global $product;
// Get the current product ID
$product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
// Define HERE your targetted products IDs in this array <=== <=== <===
$target_products_ids = array(123,152,162);
// If the current product have the same ID than one of the defined IDs in your array,…
// we remove the tab.
if(in_array($product_id, $target_products_ids)){
// KEEP BELOW ONLY THE TABS YOU NEED TO REMOVE <=== <=== <=== <===
unset( $tabs['description'] ); // (Description tab)
unset( $tabs['reviews'] ); // (Reviews tab)
unset( $tabs['additional_information'] ); // (Additional information tab)
}
return $tabs;
}
此代码可以很好地取消设置或隐藏特定产品的标签.
This code works fine to unset or hide tabs from specific products.
相反,我想取消设置或隐藏特定产品类别的标签.
我如何为产品类别执行此操作?
How can I do it for product categories?
作为最初来自我的一个答案的代码,使其适用于产品类别非常简单,只需更改 2 行并使用WordPress 条件函数 has_term():
As the code originally from one of my answers, it's very simple to make it work for product categories just changing 2 lines and using WordPress conditional function has_term():
add_filter( 'woocommerce_product_tabs', 'conditionaly_removing_product_tabs', 99 );
function conditionaly_removing_product_tabs( $tabs ) {
// Get the global product object
global $product;
// Get the current product ID
$product_id = $product->get_id();
// Define HERE your targeted categories (Ids, slugs or names) <=== <=== <===
$product_cats = array( 'clothing', 'posters' );
// If the current product have the same ID than one of the defined IDs in your array,…
// we remove the tab.
if( has_term( $product_cats, 'product_cat', $product_id ) ){
// KEEP BELOW ONLY THE TABS YOU NEED TO REMOVE <=== <=== <=== <===
unset( $tabs['description'] ); // (Description tab)
unset( $tabs['reviews'] ); // (Reviews tab)
unset( $tabs['additional_information'] ); // (Additional information tab)
}
return $tabs;
}
代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.
代码经过测试并在 WooCommerce 中运行.
The code tested and works in WooCommerce.
WordPress 条件函数 has_term() 接受您的产品类别的 ID、slug 或名称......
WordPress conditional function
has_term()accept Ids, slugs or names of your product categories…
这篇关于在woocommerce中取消特定产品类别的产品标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!
不能使用 'Object 作为类名,因为它是保留的Cannot use #39;Object as class name as it is reserved Cake 2.2.x(不能使用 Object 作为类名,因为它是保留的 Cake 2.2.x)
OAuth 重定向后会话丢失Session is lost after an OAuth redirect(OAuth 重定向后会话丢失)
Cakephp 3.x 中的分页排序Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分页排序)
CakePHP 多个应用程序的共享核心CakePHP Shared core for multiple apps(CakePHP 多个应用程序的共享核心)
在 CakePHP 3 上登录 [ Auth->identify() ] 始终为 falLogin [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登录 [ Auth-identify() ] 始终为 false)
致命错误:允许的内存大小为 134217728 字节已用尽Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命错误:允许的内存大小为 134217728 字节已用尽