• Skip to primary navigation
  • Skip to main content
dma-logo-icon
  • WordPress development
  • Managed Hosting
  • Portfolio
  • Blog
  • Contact
  • WordPress development
  • Managed Hosting
  • Portfolio
  • Blog
  • Contact
Say hello
Hide Search

Archives for April 2018

WooCommerce: how to restrict purchasing based on WordPress role

Peter Shilling · Apr 29, 2018 · Leave a Comment

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 );
$user_roles=$user_meta->roles;

if ( in_array( 'alumni', (array) $user_roles ) ) {
  return true;
}elseif( in_array( 'administrator', (array) $user_roles ) ) {
  return true;
}
else{
  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.

Enjoy!

Checkout more of our WooCommerce content here.

Say hello :)

We'd love to hear about your WordPress project! Whether it's a rebuild of an existing website, a brand new website or you're looking for a team of WordPress professionals to consult on your project, we're here to help.

dma-logo-icon
help-for-wp-logo-dma-2
© 2025 The Digital Marketing Agency
  • Plugins
  • Privacy
Chat to us about your website project!