How to Hide the Woocommerce Buy Now Button for Specific User Roles Using Ultimate Member
As an e-commerce store owner, you may want to restrict access to certain products or features based on the user's role or membership status. One common use case is hiding the WooCommerce "Buy Now" button for non-approved customers or users with specific roles.
In this blog post, we'll explore a working solution to hide the WooCommerce Buy Now button for users who are not part of the "um_member" role, using the Ultimate Member plugin.
The Challenge
The original solution provided by @LoicTheAztec had a few issues that needed to be addressed:
-
The global variable used to get the user's role had changed in the Ultimate Member 2.0 update, from $ultimatemember->user->get_role()
to UM()->user()->get_role()
. The documentation hadn't been updated yet, which caused some confusion.
-
The code was not working correctly because each member can have multiple roles (e.g., the default WordPress "subscriber" role and the Ultimate Member "member" role). The solution needed to be reworked to handle this scenario.
The Working Solution
Here's the updated solution that addresses the issues mentioned above:
$user = wp_get_current_user();
if ( in_array( 'um_member', (array) $user->roles ) )
{
// DO THIS IF USER IS A MEMBER
} else {
// DO THIS IF USER IS NOT A MEMBER
}
This code first retrieves the current user object using wp_get_current_user()
and then checks if the user has the "um_member" role in their array of roles. If the user is a member, you can proceed with the desired functionality. If the user is not a member, you can implement the alternative functionality.
To completely hide the WooCommerce Buy Now button for non-approved customers, you can use the following code:
$user = wp_get_current_user();
if ( in_array( 'um_member', (array) $user->roles ) )
{
// DO THIS IF USER IS A MEMBER
} else {
//function for deleting the Buy Now button
function remove_product_description_add_cart_button(){
global $product;
// Set HERE your category ID, slug or name (or an array)
$category = 'restricted';
//Remove Add to Cart button from product description of product with id 1234
if ( has_term( $category, 'product_cat', $product->id ) )
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
}
add_action('wp','remove_product_description_add_cart_button');
add_action( 'woocommerce_single_product_summary', 'restrict_access', 20 );
}
function restrict_access() {
echo '<div class="restrict-access"><h4>';
the_field('notice_title' , 'option');
echo '</h4><p>';
the_field('notice_details' , 'option');
echo '</p><a class="et_pb_button" href="/restricted-product">';
the_field('notice_button_text' , 'option');
echo '</a></div>';
}
In this solution, the remove_product_description_add_cart_button()
function checks if the current product belongs to a specific category (in this case, "restricted"). If the product is in the "restricted" category, the function removes the "Add to Cart" button from the single product page using the remove_action()
function.
Additionally, the restrict_access()
function is hooked to the woocommerce_single_product_summary
action, which displays a custom notice box with a title, details, and a button to redirect the user to a "Restricted Product" page. This notice box will be shown to users who are not part of the "um_member" role.
Make sure to replace the $category
variable with the appropriate category ID, slug, or name (or an array of categories) that you want to restrict access to.
Real-World Example and Statistics
Let's consider a real-world example of how this solution can be implemented and the potential impact it can have.
Imagine an e-commerce store that sells a range of products, some of which are only accessible to approved customers or members. Using the Ultimate Member plugin, the store owner can create different user roles, such as "member" and "non-member."
By implementing the solution described above, the store owner can ensure that the WooCommerce Buy Now button is hidden for non-member users, preventing them from purchasing restricted products. This can help maintain the exclusivity of certain products and ensure that only approved customers can access them.
According to a study by Baymard Institute, 68.53% of shopping carts are abandoned, and one of the main reasons is unexpected costs or fees. By hiding the Buy Now button for non-members, the store owner can reduce the likelihood of non-members attempting to purchase restricted products, leading to a smoother checkout process and potentially lower cart abandonment rates.
Furthermore, research by the Nielsen Norman Group suggests that users tend to scan web pages in an F-shaped pattern, focusing on the top and left-hand side of the page. By strategically placing the custom notice box using the restrict_access()
function, the store owner can effectively communicate the restricted access to non-member users, helping them understand the limitations and potentially guiding them towards the appropriate membership or payment options.
Conclusion
In this blog post, we've explored a working solution to hide the WooCommerce Buy Now button for specific user roles using the Ultimate Member plugin. By addressing the challenges encountered in the original solution, we've provided a comprehensive approach that effectively restricts access to certain products based on user roles.
This solution can be particularly useful for e-commerce store owners who want to maintain exclusivity for certain products or features, while still providing a seamless user experience for their approved customers or members.
Remember, when implementing this solution, be sure to replace the $category
variable with the appropriate category information for your specific use case. Additionally, you can customize the content and styling of the custom notice box to align with your brand and user experience.
For more information on how Flowpoint.ai can help you identify and address technical errors that impact your website's conversion rates, please visit Flowpoint.ai
Get a Free AI Website Audit
Automatically identify UX and content issues affecting your conversion rates with Flowpoint's comprehensive AI-driven website audit.