How to Limit Purchases by Role

Did you ever need to limit purchases for a specific product or category to some customers only?

You can do it with WooCommerce by adding a snippet to your site. Let’s see an example!

Use Case

Let’s say that I have two type of customers, regular customers, and resellers. All products are available to regular customers, but products from the category Music are not available for resellers.

How do I limit them from purchasing those products?

First of all, I need to create the role Reseller and add a generic capability to it, in our case resell_product. I did this by using the plugin User Role Editor, you can check its documentation to learn how to do it.

Once I created the role, I need to write some custom code to prevent them from purchasing from the category Music. So I open my functions.php file in wp-content/themes/my-child-theme-name/ and add this code at the end of it:

* Checks if the customer is a reseller and
* returns false if they are trying to purchase from a specific category.
* @param bool $is_purchasable If the product is purchasable or not.
* @return bool
function limit_purchases_by_role( $is_purchasable, $product ) {
$categories = $product->get_category_ids();
if ( current_user_can( 'resell_product' ) && in_array( 17, $categories ) ) {
return false;
return $is_purchasable;
add_filter( 'woocommerce_is_purchasable', 'limit_purchases_by_role', 10, 2 );

view raw


hosted with ❤ by GitHub

On line 11 there are two checks:

current_user_can( 'resell_product' ) && in_array( 17, $categories )

The first one checks if the current logged in users is a reseller, based on a custom capability that I added to the role Reseller. The second one checks if the product is in the category with ID 17, which is the ID of the category Music, the one that I want to block the purchases for.

With this code in the file I can’t even see the Add to Cart button on the product page:

The Add to Cart button is missing from the product page.
The Add to Cart button is missing from the product page.

While on the Shop page I see a Read More button:

And if I click on it, I will be redirected to the Shop page without being able to buy the product.

More Posts That You Might Like…

5 responses to “How to Limit Purchases by Role”

  1. if I have a user role of Pro DJ it would be what? prodj? Because I tried that and this didn’t work.

    1. I’m not sure to understand what you mean.

  2. This is great, Thank you.

    How do I restrict the admin from totally purchasing any product?

  3. […] Also, if you are you looking for how to limit purchases by role I wrote about that in this article! […]

  4. Humayun Ahmed Avatar

    Thanks for your article.
    how to add any notice when click ad to card button?

Leave a Reply



Receive new articles from this blog directly in your inbox!

No spam guaranteed!

Blog at

%d bloggers like this: