WooCommerce Hide Specific Products for Certain Users – 2024

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.
See also  WooCommerce: Bulk Dynamic Pricing (Without Plugins) - 2024

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 the if 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.

See also  WooCommerce Dynamic Pricing Based on Cart Contents - 2024

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.

0 0 votes
Article Rating

Stay In Touch

Was this post helpful? Why not show your support and buy me a coffee?

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Scroll to Top
×