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

时间:2019-12-18 05:48:11

标签: php wordpress woocommerce

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

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

 if (pickedImage == null) {
   // File is null, probably not found or incorrect path.
   return;
 }

 // Do something with the file
 if (pickedImage.path == null) {

 }

谢谢

2 个答案:

答案 0 :(得分:1)

尝试这样的事情...

// 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;
}

答案 1 :(得分:0)

请使用下面的代码进行检查。代替YOUR_PAGE_SLUG,您需要替换为页面标签,并将“ woo”替换为您需要隐藏的类别的产品类别标签

add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 );
function get_subcategory_terms( $terms, $taxonomies, $args ) {
  $new_terms = array();
  // if a product category and on the shop page
 // to hide from shop page, replace is_page('YOUR_PAGE_SLUG') with is_shop()
  if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_page('YOUR_PAGE_SLUG') ) {
    foreach ( $terms as $key => $term ) {
      if ( ! in_array( $term->slug, array( 'woo' ) ) ) {
        $new_terms[] = $term;
      }
    }
    $terms = $new_terms;
  }
  return $terms;
}