how can I exclude woo product categories only in the front of the shop with the function? Can anyone help me?
Asked
Active
Viewed 258 times
1 Answers
1
The following code hides categories on the store page and the accordion using the ID and name of each category.
For the accordion, the category ID must be used, which will be placed where it says array ('1', '2'); and for the store you must use the name of the category and it will be placed where it says array ('category-name', 'category-name')
// Hide in Accordion
add_filter( 'woocommerce_product_categories_widget_dropdown_args', 'oaf_wc_exclude_categories_from_list_widget' );
add_filter( 'woocommerce_product_categories_widget_args', 'oaf_wc_exclude_categories_from_list_widget' );
function oaf_wc_exclude_categories_from_list_widget( $cat_args ) {
$cat_args['exclude'] = array('1','2'); // array with categories ids to exclude
return $cat_args;
}
// Hide in Store
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
if ( in_array( 'product_cat', $taxonomies ) && ! is_admin() && is_shop() ) {
foreach ( $terms as $key => $term ) {
if ( ! in_array( $term->slug, array( 'name-category', 'name-category' ) ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}
Tested and working, the code goes to the end in the fuctions.php file of your theme o your child theme.
Here you have an image where you can see where to find the id and name of categories. Regards.

Williams
- 421
- 4
- 18