Restrict Products for Sale Conditionally in WooCommerce

These code snippets enable you to restrict the sale of 1 or more products based on specific conditions in WooCommerce. You can control who buys products by :

  • User role – In this case we use the contributor use role so only logged in users assigned this role can purchase a specific product. In this case, its product id 394.
  • Customers who have purchased another product(s). In this case, customers who have purchased product id 392 can purchase product 394.

Demo Video

Shows product 394 restricted for sale only to customers who have already purchased product 392 otherwise a warning notice is displayed preventing checkout until they have purchased product id 392. Once they have, they can then checkout the restricted product.

Restrict by User Role

The following code restricts the sale of products by user role. In this example, anyone with the contributor user role can purchase product id 394. No-one else can.

Restrict by Another Product Purchased

The following code restricts the sale of products by customers who have purchased another product. In this example, anyone who has purchased product id 392 can purchase product id 394. No-one else can.

Usage

All you need to do is swap out the user role and/or product ids to match your own.

Was This Tutorial Helpful?

Free

$0

Access only to all free tutorials per month.



Monthly

$75

Access to 10 premium tutorials per month.


Tutorial Request


Includes code guarantee and coding support.

Yearly

$500

Access to 15 premium tutorials per month.


Monthly Tutorial Request


Includes code guarantee and priority coding support.