WooCommerce: How to Hide a Product From All User Roles (The Ultimate Guide)
Introduction:
WooCommerce is a powerful platform for building online stores, offering a wide range of customization options. Sometimes, you might need to hide a product from *all* user roles, meaning it won’t be visible to customers, guests, or even administrators under specific circumstances. This could be for various reasons, such as:
- Temporarily discontinuing a product.
- Preparing a product for a future launch.
- Offering a product only through specific promotions or channels.
- Handling product availability issues.
- For temporary product removal or situations where you don’t want the product easily discoverable but still accessible via a direct link.
- When you need to completely remove a product from public view while keeping its data intact within the backend.
This article will guide you through several methods to achieve this, covering different levels of complexity and suitability for your specific needs. Whether you’re a beginner or a seasoned WooCommerce user, you’ll find a solution here.
Main Part: Methods to Hide a Product
There are several approaches to hiding a product from all user roles in WooCommerce. We’ll explore each in detail, along with code examples and considerations:
1. Setting Product Visibility to “Hidden”
This is the simplest method and a good starting point for basic needs. It involves changing the product’s visibility setting directly within the WooCommerce product edit screen.
How to do it:
1. Go to Products > All Products in your WordPress admin dashboard.
2. Find the product you want to hide and click Edit.
3. Locate the Publish meta box (usually in the top right corner).
4. Click the Edit link next to “Visibility.”
5. Select Hidden from the options.
6. Click OK and then Update the product.
What it does:
This setting prevents the product from appearing in your shop catalog, category pages, and search results. However, *users with the direct product URL can still access the product page*.
When to use it:
2. Changing Product Status to “Draft”
Changing the product status to “Draft” effectively removes it from the frontend of your site. It’s similar to “Hidden,” but more complete.
How to do it:
1. Go to Products > All Products in your WordPress admin dashboard.
2. Find the product you want to hide and click Edit.
3. Locate the Publish meta box.
4. Change the status from “Published” to Draft.
5. Click Update.
What it does:
The product is no longer visible on the frontend, and even accessing the direct product URL will result in a 404 error (page not found) or redirect.
When to use it:
3. Using Code Snippets (Advanced)
For more robust and granular control, you can use code snippets to prevent access to specific products. This method involves adding custom PHP code to your theme’s `functions.php` file or using a code snippets plugin (recommended). *Always backup your site before making changes to your theme files.*
A. Redirecting Users Away From the Product Page
This snippet redirects users accessing a specific Check out this post: How To Check Product Category In Woocommerce product page to a different page (e.g., the shop page).
function redirect_hidden_product() { global $product;
// Replace ‘123’ with the actual product ID you want to hide
$product_id_to_hide = 123;
if ( is_product() && $product->get_id() == $product_id_to_hide ) {
wp_redirect( get_permalink( wc_get_page_id( ‘shop’ ) ) ); // Redirect to the shop page
exit;
}
}
add_action( ‘template_redirect’, ‘redirect_hidden_product’ );
Explanation:
- `is_product()`: Checks if the current page is a single product page.
- `$product->get_id()`: Retrieves the ID of the current product.
- `wp_redirect()`: Performs the redirection to the specified URL.
- `wc_get_page_id( ‘shop’ )`: Gets the ID of the shop page to ensure redirection always works.
B. Removing the Product From Queries:
This snippet prevents the product from being included in WooCommerce queries, effectively hiding it from shop listings, category pages, and search results.
function exclude_product_from_queries( $query ) { if ( ! is_admin() && $query->is_main_query() && ( is_shop() || is_product_category() Check out this post: How To Change Woocommerce Products Per Page With Divi || is_product_tag() ) ) { // Replace '123' with the actual product ID you want to hide $product_id_to_hide = 123;
$query->set( ‘post__not_in’, array( $product_id_to_hide ) );
}
}
add_action( ‘pre_get_posts’, ‘exclude_product_from_queries’ );
Explanation:
- `! is_admin()`: Ensures the code only runs on the frontend.
- `$query->is_main_query()`: Checks if it’s the main query being executed.
- `is_shop() || is_product_category() || is_product_tag()`: Ensures the code only runs on shop, category, and tag pages.
- `$query->set( ‘post__not_in’, array( $product_id_to_hide ) )`: Excludes the specified product ID from the query results.
When to use code snippets:
- When you need very specific control over product visibility.
- When you need to automate the hiding process based on certain conditions.
- When you need to prevent access even with the direct product URL.
4. Using Plugins
Several WooCommerce plugins offer advanced product visibility control. These plugins often provide a user-friendly interface for managing which user roles can see specific products. Examples include:
- WooCommerce Visibility Options: Allows you to control product visibility based on user roles, categories, and more.
- User Role Editor: While primarily for managing user roles, some versions offer limited product visibility control.
When to use plugins:
- When you need a user-friendly interface for managing complex visibility rules.
- When you don’t want to write custom code.
Conslusion:
Choosing the right method for hiding a product from all user roles in WooCommerce depends on your specific needs and technical expertise. For simple tasks, setting the visibility to “Hidden” or changing the status to “Draft” is sufficient. However, for more granular control and complete removal, code snippets or dedicated plugins are the better choice. *Always remember to back up your site before making any changes to your theme files or installing new plugins*. By using these techniques effectively, you can manage your product catalog efficiently and ensure that only the intended audience has access to specific items.