从 WooCommerce 产品类别小部件中删除父类别和子类别

用户2766888

我有以下代码从 WooCommerce 产品类别小部件中为除管理员和批发客户(自定义用户级别)之外的所有用户删除“批发类别”(通过 slug)。

这很好用,但是它只会删除父类别并保留所有子类别。如何删除“批发类别”的子类别?

// Woocommerce - Removes category link from woocommerce product category widgets so they are not seen
add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );

function get_subcategory_terms( $terms, $taxonomies, $args ) {

  $new_terms = array();

  $category_to_check = get_term_by( 'slug', 'wholesale-category', 'product_cat' );

  // if a product category and on the shop page
  if ( in_array( 'product_cat', $taxonomies ) && ! ( current_user_can( 'wholesale_customer' ) || current_user_can( 'administrator' ) ) ) {

    foreach ( $terms as $key => $term ) {

      if ( ! in_array( $term->slug, array( 'wholesale-category' ) ) ) {
        $new_terms[] = $term;
      }

    }

    $terms = $new_terms;

  }

  return $terms;
}

谢谢

雷格尔

尝试这样的事情......

// Woocommerce - Removes category link from woocommerce product category widgets so they are not seen
add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );

function get_subcategory_terms( $terms, $taxonomies, $args ) {

  $category_to_check = get_term_by( 'slug', 'wholesale-category', 'product_cat' );
  if ( in_array( 'product_cat', $taxonomies ) && ! ( current_user_can( 'wholesale_customer' ) || current_user_can( 'administrator' ) ) ) {
    foreach ( $terms as $key => $term ) {
      if ( in_array( $term->slug, array( $category_to_check->slug ) ) || ( $category_to_check->term_id == $term->parent ) ) {
        unset($terms[$key]);
      }
    }
  }
  return $terms;
}

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

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

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

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

在Woocommerce中基于产品类别隐藏价格

从WooCommerce的订单项中获取产品类别ID

在Woocommerce单一产品页面中获取产品类别名称和描述

在Woocommerce中根据产品类别更改货币符号

从Woocommerce中的子产品类别ID获取父产品类别ID

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

从Woocommerce类别下拉小部件中禁用某些产品类别

从显示类别为Woocommerce的子类别的产品类别中删除边栏

列出WooCommerce中产品类别的主要产品子类别

在Woocommerce中删除产品类别的占位符图像

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

在Woocommerce中按产品类别,产品标签和价格查询

从产品类别中排除最近查看的产品小部件中的Woocommerce产品

从WooCommerce中的“相关产品”中排除产品类别

在自定义家庭和产品类别档案中显示WooCommerce产品属性

获取WooCommerce中的所有产品类别

定位特定的产品类别,它是WooCommerce中的子类别

在Woocommerce档案中获取当前产品类别的子类别

如何在侧边栏小部件中修改和设置WooCommerce产品类别计数NUMBER的样式

如果产品类别有子级,则从WooCommerce的父条款中删除永久链接

Woocommerce:产品类别小部件

woocommerce产品类别窗口小部件禁用父类别链接

隐藏woocommerce类别小部件中的子类别

woocommerce获取产品类别ID

更改WooCommerce产品类别标题?

Woocommerce 特定的横幅产品类别和子项

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

Woocommerce的分层产品类别插件