Posted on

How to Hide a Category in WooCommerce Without Plugins?

Know who needs to hide product categories on their WooCommerce store and how they can do it via some code.

WooCommerce hide category

Last updated on June 8, 2023

Why hide product categories in your store? Does it affect conversions?

If so, how to do it?

In this ‘WooCommerce hide category’ blog, we’ll list some code snippets to hide categories on your shop page and other scenarios. This will make it easier for customers to navigate, and keep your homepage clean, thus improving conversions.

Why hide categories in WooCommerce?

WooCommerce categories help you organize your products so that users can easily find what they’re looking for.

However, not all categories need to be shown to the users. For example, the ‘Uncategorized’ category in WooCommerce.

Or you want to prevent certain products from being displayed in your shop. For example:

  • Create a member-only store that allows only special and logged-in users to purchase the products.
  • Sell products to specific users only, allowing them to access them after using a specific password.
  • Create private products and send access to some of your customers via email.

Note – Hiding categories will not remove them from your website entirely. Any product assigned a category can be accessed via direct links and search engines. To completely remove a category, you need to delete it entirely.

You can also bulk remove and assign categories to products using Smart Manager plugin.

WooCommerce hide category – doing it the default way and coding

You can use your default WooCommerce settings to hide categories in WooCommerce.

In some cases, you’ll need some code snippets. Let’s look at the various scenarios:

Your visitors won’t be able to see that category or products belonging to it.

Hide all categories on the shop page

  • Go to WordPress Admin panel > Appearance > Customize > WooCommerce > Product Catalog.
  • Under the Shop page display, choose Show products and click Publish.
hide categories shop page

This will show all the products only and not the categories.

Hide categories in product widgets

If you want to hide your current category from the product categories widget, use the following code in your functions.php file.

But adding code is always risky. Refer to our doc on safely adding code snippets before you move ahead.

add_filter( 'woocommerce_product_categories_widget_args', 'storeapps_hide_current_cat_prod_cat_widget' );

function storeapps_hide_current_cat_prod_cat_widget( $args = array() ) {
if ( is_product_category() ) {
$current_cat_id = get_queried_object_id();
$args['exclude'] = $current_cat_id;
}
return $args;
}

Hide categories from users

This if you want to allow access for some categories only to specific users. The below code restricts users having the role Subscriber and Shop Manager from accessing the Fashion category.

add_filter( 'woocommerce_product_query_tax_query', 'storeapps_hide_shop_categories_by_role' );
function storeapps_hide_shop_categories_by_role( $tax_query = array() ) {
$user = wp_get_current_user(); // Get the current user object.
$blocked_user_roles = array( 'subscriber', 'shopmanager' ); // Define an array of blocked user roles.
$hidden_categories = array( 'fashion' ); // Define an array of hidden categories.

if ( is_user_logged_in() && count( array_intersect( $blocked_user_roles, $user->roles ) ) === 0 ) {
return $tax_query;
}

$tax_query[] = array(
'taxonomy' => 'product_cat',
'terms' => $hidden_categories,
'field' => 'slug',
'operator' => 'NOT IN',
);

return $tax_query; // Return the modified tax query.
}

Hide products from a WooCommerce product category

This is to remove products from categories instead of removing categories in the first place.

add_action( 'woocommerce_product_query', 'storeapps_exclude_categories_from_shop_page' );
function storeapps_exclude_categories_from_shop_page( $q ) {
$tax_query = (array) $q->get( 'tax_query' );
$tax_query[] = array(
'taxonomy' => 'product_cat',
'field' => 'slug',
'terms' => array( 'uncategorized', 'books' ), // Exclude products in the "uncategorized" and "books" categories.
'operator' => 'NOT IN'
);
$q->set( 'tax_query', $tax_query );
}

Ending notes

That’s it from our WooCommerce hide category blog post.

Hiding categories is quite useful to make your shop page easier to use and manage, and also makes for a better customer experience.

Would you like us to add more code for different scenarios and plugins to hide categories? Reply in the box below.

Leave a Reply

Your email address will not be published. Required fields are marked *

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