WooCommerce: How to Hide Categories From Your Online Store
Introduction:
WooCommerce is a powerful and flexible platform for building online stores. One common requirement for store owners is the ability to hide specific product categories from their front-end display. This might be for various reasons: temporary promotions, internal categories for grouped products, wholesale-only categories, or categories containing products you want to showcase individually. Whatever the reason, knowing how to hide WooCommerce categories is essential for managing your store effectively. This article will guide you through several methods, from simple settings to more advanced code solutions.
Main Part: Hiding WooCommerce Categories – Various Methods
There are several ways to hide WooCommerce categories, each with its own pros and cons. We’ll cover the most popular and effective options:
1. Hiding Categories Using the WooCommerce Category Visibility Setting
This is the simplest method and ideal for basic hiding needs. It allows you to hide a category from the shop page and search results but doesn’t completely remove it from your website.
- Steps:
- Limitations:
- The category will still be accessible if someone has a direct link to it.
- The category is not completely removed from the database, which may affect some plugins or advanced functionalities.
- This only affects product archive and category pages, not specific products within that category.
- Hiding Categories from the Shop Loop:
1. Log into your WordPress dashboard.
2. Navigate to Products > Categories.
3. Hover over the category you want to hide and click Edit.
4. In the “Edit Category” screen, find the “Visibility” option. It’s usually located on the left side panel.
5. Select “Hidden” from the dropdown menu.
6. Click “Update” to save your changes.
2. Hiding Categories with Custom Code Snippets
For more robust and permanent hiding, you can use custom code snippets. This is a more advanced method, but it provides greater control.
This code snippet removes specific categories from the main shop loop. Replace `’category-slug-1′, ‘category-slug-2’` with the actual slugs of the categories you want to hide.
function custom_pre_get_posts_query( $query ) { if ( ! is_admin() && $query->is_main_query() && is_shop() ) { $query->set( 'tax_query', array( array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => array( 'category-slug-1', 'category-slug-2' ), 'operator' => 'NOT IN', ) )); } } add_action( 'pre_get_posts', 'custom_pre_get_posts_query' );
Explanation:
- `is_admin()`: Ensures the code only runs on the front end, not in the admin panel.
- `$query->is_main_query()`: Targets the main product query on the shop page.
- `is_shop()`: Checks if we are on the shop page.
- `tax_query`: A powerful tool for modifying taxonomy queries. We’re using it to exclude the specified categories.
- `’operator’ => ‘NOT IN’`: Specifies that we want to *exclude* the categories listed in the `terms` array.
- Hiding Categories from Category Widgets and Menus:
You can filter the categories that appear in WooCommerce widgets and menus. Again, replace `’category-slug-1′, ‘category-slug-2’` with your category slugs.
function custom_product_category_widget_args( $args ) { $exclude_categories = array( 'category-slug-1', 'category-slug-2' ); $args['exclude'] = implode( ',', get_terms( array( 'taxonomy' => 'product_cat', 'slug' => $exclude_categories, 'fields' => 'ids', ) ) ); return $args; } add_filter( 'woocommerce_product_categories_widget_args', 'custom_product_category_widget_args' );
Explanation:
- `woocommerce_product_categories_widget_args`: This filter allows you to modify the arguments passed to the product categories widget.
- `get_terms()`: Retrieves term IDs (category IDs) based on the provided slugs.
- `implode()`: Combines the array of IDs into a comma-separated string, which is the expected format for the `exclude` argument.
- Where to add these code snippets:
The best place to add these code snippets is in your theme’s `functions.php` file or in a custom plugin. Important: Always back up your website before modifying the `functions.php` file! Using a child theme is recommended to avoid losing your changes when the parent theme is updated.
3. Using WooCommerce Plugins to Hide Categories
Several plugins offer dedicated features for hiding categories and managing category visibility. Some popular options include:
- Category Visibility WooCommerce: Provides fine-grained control over category visibility for different user roles.
- WooCommerce Hide Products: Allows you to hide products and categories based on various criteria.
- Product Visibility Manager for WooCommerce: Offers advanced options for managing product and category visibility.
- Pros of Using Plugins:
- User-friendly interface.
- Often includes additional features beyond simple hiding.
- No coding required.
- Cons of Using Plugins:
- Can add extra overhead to your website (consider plugin quality and performance).
- May require payment for premium features.
- Potential conflicts with other plugins.
Conclusion:
Hiding WooCommerce categories is a valuable skill for managing your online store effectively. Whether you choose the simple “Hidden” visibility setting, custom code snippets, or a dedicated plugin, the best method depends on your specific needs and technical comfort level. Remember to test your changes thoroughly to ensure that your categories are hidden as intended and that your website functions correctly. Using code snippets can provide the most precise control, but plugins offer user-friendly solutions for those less comfortable with coding. Ultimately, the goal is to create a seamless and intuitive shopping experience for your customers while maintaining control over your product catalog.