Hi guys.
I’d like you to suggest me a snippet to hide a specific category from the shop.
I tried these unsuccessfully:
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( 'cat-to-remove' ) ) ) {
$new_terms[] = $term;
}
}
$terms = $new_terms;
}
return $terms;
}
add_filter( 'pre_get_posts', 'custom_pre_get_posts_query' );
function custom_pre_get_posts_query( $q ) {
if ( ! $q->is_main_query() ) return;
if ( ! $q->is_post_type_archive() ) return;
$q->set( 'tax_query', array(array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'cat-to-remove' ),
'operator' => 'NOT IN'
)));
remove_filter( 'pre_get_posts', 'custom_pre_get_posts_query' );
}
and
function exclude_categories($args){
$exclude = "123";
$args["exclude"] = $exclude;
return $args;
}
add_filter("woocommerce_product_subcategories_args","exclude_categories");
My need is to have a nth category that shouldn’t appear anywhere – shop and product pages – in the shop but that works effectively: I have to apply a specific coupon discount.
Have you ever had to manage this?
I hope so, that’s why I asked for your help.