Hiding specific products for certain users is a useful strategy in WooCommerce to create exclusive content or products tailored to specific customer groups. Whether it’s special merchandise available only to members or products targeted at specific professions, this functionality enhances user experience and can drive more targeted sales. This tutorial will walk you through how to implement this in WooCommerce by adding custom code to your theme’s functions.php
file.
Prerequisites
Ensure you have administrative access to your WordPress site and are comfortable with editing PHP files. Implementing these changes in a child theme is recommended to avoid losing them during theme updates.
Step 1: Define User Role and Product Criteria
Before writing any code, identify which user roles should be restricted from viewing certain products and which products need to be hidden. For instance, you might want to hide certain premium products from non-subscribers.
Step 2: Add Custom Code to Your Theme’s functions.php
File
Add the following code to your theme’s functions.php
file to hide specific products for certain users based on user roles:
/*
* Snippet: WooCommerce Hide Specific Products for Certain Users – 2024
* Author: John Cook
* URL: https://wcsuccessacademy.com/?983
* Tested with WooCommerce 8.8.3
*/
// WooCommerce Hide Specific Products for Certain Users
add_action('pre_get_posts', 'wcsuccess_hide_specific_products');
function wcsuccess_hide_specific_products($query) {
if (is_admin() || !$query->is_main_query() || !is_shop() && !is_product_category() && !is_product_tag()) {
return;
}
if (current_user_can('subscriber')) { // Change to the desired user role
$excluded_ids = array(25, 30); // Add the IDs of products to hide
$query->set('post__not_in', $excluded_ids);
}
}
Explanation of the Code
- Hook into Query: We use the
pre_get_posts
action to modify the main query that WooCommerce uses to fetch products on shop, category, and tag pages. - Check Conditions: The function first checks if the current context is appropriate (not admin, main query, and on relevant WooCommerce pages).
- User Role Check: Adjust the condition
current_user_can('subscriber')
to match the specific user role you’re targeting. - Excluding Products:
post__not_in
is set with an array of product IDs that should not be displayed to the specified users.
Step 3: Test Your Product Visibility Restrictions
After implementing the code:
- Log in with an account that matches the restricted user role.
- Navigate to your shop or product category pages.
- Verify that the specified products are not visible.
- Test with an unrestricted account to ensure those products remain visible to other users.
Customising Further
- Multiple User Roles: Expand the conditions to include multiple user roles using
||
(OR) conditions within theif
statement. - Dynamic Product Lists: Retrieve product IDs dynamically based on product categories or tags if the list needs frequent updating.
Conclusion
WooCommerce hide specific products for certain users is an effective way to manage product visibility and offer exclusive content to selected customer groups. This capability not only enhances the shopping experience for privileged users but also helps in managing inventory and promotions targeted at specific segments.
Testing changes on a staging environment before applying them to your live site is crucial to ensure that the implementation works as expected and does not adversely affect the user experience for different customer segments. This guide on how to hide specific products for certain users should help you customise your WooCommerce store for a more targeted and engaging customer experience.
I have been working with WordPress and WooCommerce since 2012 and have developed a deep knowledge of the content management system. Since 2012, I have developed several plugins and designed dozens of websites utilising different frameworks, CMS’s and programming languages. I am proficient in PHP, Python, Java, C, C++, R and JavaScript with limited experience in Go, Kotlin and Swift.
Educationally, I have a Master’s degree in cyber security a Bachelor’s (Hons, First Class) in Applied Research and a Graduate Certificate in Data Science. I’m currently undertaking PhD studies investigating IoT cybersecurity. I recently graduated with First Class Honours and Masters of Information Technology, receiving the Executive Dean’s Award for studies undertaken in the 2021 and 2022 academic years. I have worked in the information technology industry for the past 11 years primarily as a software/web developer specific to design, optimisation, network management and security. My research interests are in the areas of Internet of Things (IoT), 5G and Beyond Networks, information security for wireless networks and software development.
Google Scholar ID: https://scholar.google.com.au/citations?user=0KvpwNEAAAAJ&hl=en
ORCID: https://orcid.org/0000-0001-8113-1296
Stay In Touch