Search the site:

Home Blog How to hide a WooCommerce product category on the Shop Page

How to hide a WooCommerce product category on the Shop Page

We have seen how you can hide a product category from getting displayed using the [product_categories] shortcode. But what if you want to hide a product category on the Shop Page, after displaying it using the WordPress Customizer? In this post, you will learn how you can hide a WooCommerce product category on the Shop Page, as well as hide products from a particular category.

An easy way to display product categories or products on the WooCommerce Shop Page is by using the Customizer which you can access by clicking on the Customize option on the top left while on the Shop Page, and choosing WooCommerce->Product Catalog:

How to hide a WooCommerce product category on the Shop Page - WooCommerce Product Catalog

Inside the Product Catalog option, you can choose what to display on the Shop Page, whether it’s Products , Product Categories or both Product Categories & Products.

How to hide a WooCommerce product category on the Shop Page - How to display Categories & Products on the Shop Page

 

Hiding a WooCommerce product category on the Shop Page

Suppose you don’t want to show a particular category. For the purpose of this tutorial, let us consider hiding the Uncategorised category. We will start with finding a slug for this category. You can do this by navigating to Products->Categories in your WordPress admin dashboard.

How to hide a WooCommerce product category on the Shop Page - Finding category slugs

We can see that the slug for the Uncategorised category is ‘uncategorised’ itself.

Add the below code snippet to the functions.php  file of your child theme in order to make this category hidden.

add_filter( 'get_terms', 'ts_get_subcategory_terms', 10, 3 );

    function ts_get_subcategory_terms( $terms, $taxonomies, $args ) {

          $new_terms = array();

          // if it is 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( 'uncategorised' ) ) ) {        //pass the slug name here
                    $new_terms[] = $term;
                 }
          }
          $terms = $new_terms;
    }

    return $terms;
}

We have added a function to the get_terms filter. You can now see that the category “Uncategorised” is no longer visible as it was earlier.

How to hide a WooCommerce product category on the Shop Page - Category Uncategorised hidden on Shop Page

You can insert slug names of all the categories you want to hide, separated by a comma:

add_filter( 'get_terms', 'ts_get_subcategory_terms', 10, 3 );

    function ts_get_subcategory_terms( $terms, $taxonomies, $args ) {

          $new_terms = array();

          // if it is 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( 'uncategorised','furniture' ) ) ) {        //pass the slug name here
                    $new_terms[] = $term;
                 }
          }
          $terms = $new_terms;
    }

    return $terms;
}

This code snippet will hide categories with slugs ‘uncategorised’ and ‘furniture’:

How to hide a WooCommerce product category on the Shop Page - Categories UnCategorised and Furniture hidden on Shop Page

Hiding products from a WooCommerce product category on the Shop Page

If you have chosen to show both Products & Product Categories like we have in this example, you would see the products right after the Categories have been displayed.

How to hide a WooCommerce product category on the Shop Page - Categories and Products both displayed on the Shop Page

Suppose you don’t want to show the products from a particular category after hiding the category as well. In our case, we have hidden the ‘uncategorised’ and the ‘furniture’ categories, so let us also hide products from these categories from getting displayed. The screenshot above shows two products i.e. Balcony Lounger & Bar Stool from the category Furniture.

Let’s add the code snippet below to the functions.php file of the child theme:

add_action( 'woocommerce_product_query', 'ts_custom_pre_get_posts_query' ); 

function ts_custom_pre_get_posts_query( $q ) {

    $tax_query = (array) $q->get( 'tax_query' );

    $tax_query[] = array(
           'taxonomy' => 'product_cat',
           'field' => 'slug',
           'terms' => array( 'uncategorised','furniture'), // Don't display products in the clothing category on the shop page.
           'operator' => 'NOT IN'
    );


    $q->set( 'tax_query', $tax_query );

}

The products in the categories Furniture as well as Uncategorised are no longer visible. To also hide the categories from getting displayed, you can use the code snippet mentioned earlier in this post.

How to hide a WooCommerce product category on the Shop Page - Products from certain categories hidden on the Shop Page

In this way, you can use code snippets to hide both categories and products from categories, on the Shop Page in WooCommerce.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of
from purchased

Subscribe Now