取消设置woocommerce中特定产品类别的产品选项卡

MSK伊比亚斯

我正在使用来自此答案的代码:

仅在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;

}

此代码可以很好地设置或隐藏特定产品的标签。

相反,我想取消设置或隐藏特定产品类别中的标签。

如何针对产品类别执行此操作?

LoicTheAztec

正如我的答案之一中的代码一样,仅更改2行并使用WordPress条件函数,使其适用于产品类别非常简单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 = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->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中使用。

WordPress条件函数has_term()接受您产品类别的ID,ds或名称…

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

获取Woocommerce中特定产品类别的特定产品属性值

更改特定产品类别的WooCommerce产品数量设置

用Woocommerce 3中特定产品类别的产品替换加售商品

隐藏Woocommerce中具有特定产品类别的产品

显示Woocommerce中特定产品类别的产品

在Woocommerce中仅允许购买特定产品类别的一种产品

避免在Woocommerce中更新特定产品类别的购物车价格

WooCommerce中特定产品类别的小数步长

删除Woocommerce中针对特定产品类别的添加购物车按钮

更改 Woocommerce 中特定产品类别的购物车项目价格

WooCommerce中特定产品类别的最小购物车项目数量

WooCommerce中特定产品类别的最小购物车数量

在 WooCommerce 中输出特定产品类别的自定义短代码

隐藏 WooCommerce 购物车中特定产品类别的 woocommerce-checkout-review-order 表

列出Woocommerce中给定产品类别的子类别

基于特定产品类别的WooCommerce结帐消息

WooCommerce特定产品类别的渐进式数量折扣

WooCommerce禁止特定产品类别的ClearPay付款

Woocommerce-根据所选产品类别重命名产品选项卡

WooCommerce:CSS-定位特定产品类别

从 Woocommerce 的特定产品类别中删除“未找到产品”

检查产品是否属于 Woocommerce 中的特定产品类别

WooCommerce 中特定产品类别的自定义条款和条件复选框

更改Woocommerce 3中特定产品类别的“添加到购物车”文本

删除WooCommerce 3中特定产品类别的“添加到购物车”按钮

隐藏Woocommerce 3中特定产品类别的购物车优惠券字段

为Woocommerce中已定义产品类别的特定项目计数设置零税种

在Woocommerce中检查产品类别的产品

在Woocommerce中为特定产品类别添加自定义按钮