WooCommerce: how to restrict purchasing based on WordPress role

This short example shows how to limit the ability to purchase products based on a WordPress role

WordPress Tech

In this short tutorial I’ll share the solution to a problem we came across while working on a WooCommerce project. I came across a requirement that I’ve not attacked before. Now that it’s done though, it looks like the kind of thing that others might be interested in. So here goes!.

WooCommerce Restrict Purchasing

After having a look around, sure, there were plugins that did this but also loads of other things that I didn’t need. If you’re confident with a little code, here’s how to approach it. It’s based around a filter that WooCommerce offers woocommerce_is_purchasable.

In this example I have a role ‘alumni’ which is specific to the project, that could be something like subscriber etc.. depending on the situation

// Restrict the ability to buy products based on the user's role
// Role alumni or administrator is able to purchase products

function dma_restrict_product(){

$user = wp_get_current_user();
$user_meta=get_userdata( $user->ID );

if ( in_array( 'alumni', (array) $user_roles ) ) {
  return true;
}elseif( in_array( 'administrator', (array) $user_roles ) ) {
  return true;
  return false;

add_filter( 'woocommerce_is_purchasable', 'dma_restrict_product' );

The result of this will be that the products are still displayed as you would expect on the front end of your WooCommerce store, however only users setup in this function would be able to add the products to their cart and checkout.


Checkout more of our WooCommerce content here.

Reader Interactions

Leave a Reply

Your email address will not be published. Required fields are marked *