Hide certain product category in wordpress/woosommerce

I have tried to hide a certain subcategory from the category widget on shop page and other pages, there is a category widget. I found that the code below works for all pages except the shop page. If I change is_product_category() for the is_shop(), so it works for the shop page, but not for the other pages.

How can I do that it works for ALL pages (shop and all others)?

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_product_category() ) {

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

      if ( ! in_array( $term->slug, array( 'seinakellad','nastennye-chasy','wall-clock' ) ) ) {
        $new_terms[] = $term;
      }

    }

    $terms = $new_terms;
  }

  return $terms;
}

Solutions

Just remove the additional check in your if condition:

add_filter( 'get_terms', 'filter_get_terms', 10, 3 );
function filter_get_terms( $terms, $taxonomies, $args ) {
    $new_terms = [];

    // if a product category and on the shop page
    if ( ! is_admin() ) {
        foreach ( $terms as $term ) {
            if ( ! in_array( $term->slug, [ 'seinakellad', 'nastennye-chasy', 'wall-clock' ] ) ) {
                $new_terms[] = $term;
            }
        }

        $terms = $new_terms;
    }

    return $terms;
}

Similar questions

Change /product-category/category/name to /product-category/name
My product category urls currently look like: /product-category/category/category-name Where the category-name is the only changing part. I would like to change /product-category/category to just /product-category, so my final url would look like: /product-category/category-name Most of my google searches have yielded results relating to removing /...
Get Category name/slug for wp_Query on product-category to show product list according to category
I am developing a woocommerce website, and i want to show product list according to a category on the product-category page i.e: www.wesiteName.com/product-category/ladies. I want to show all the ladies products on this link but how? My code didn't work. I am using this function woocommerce_page_title() to get category name but it print the categor...
How can I hide/show a link on a product page in woocommerce depending on its product category?
I am trying to show/hide a link dependent upon the product category in woocommerce. Basically, if the product is in the "Auction" category, I want a link to display that will take the viewer to the item on the auction website. I really didn't know where to start, but I have done this before to change css on scroll, so I was going to try and take th...
Hide Add To Cart Button based on product custom field and product category in WooCommerce
I would like to add a custom check box to the woocommerce product page back end that hides the Add To Cart button in the front end. I don't want to remove the ability to purchase the item entirely (would still like to be able to use a direct add to cart url to add the item to a cart and purchase) so I don't want to use add_filter('woocommerce_is_pu...
Hide or remove the quantity field from WooCommerce Product filter in product-category page
Every time I click on a category, it directs to product-category/name_of_ category page. I have a sidebar that displays filters for products in that category. By default, it shows the quantity of product that match the filters. I want to remove that quantity. Please guide me. Thanks.
Wordpress hide or show category from loop in certain cases
I want to exclude a certain category by default in the list of posts shown on the front page. I found out how I can do this neatly with the pre_get_posts hook. It works fine, and the category posts don' show. Now I want to show these category posts ONLY if I query specifically for this category. So either all posts not in this category are shown (d...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.