I have the following code that removes the “wholesale-category” (via slug) from the WooCommerce Product Category widget for all users except for administrators and wholesale_customer (custom user level).
This works great, however it only removes the parent category and leaves all child categories. How can I remove the child categories of “wholesale-category” too?
// 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;
}
Thanks
2
Answers
Please check with below code that will do the work. in place of YOUR_PAGE_SLUG you need to replace with your page slug and replace “woo” with the product category slug of the category you need hidden
try something like this…