Woocommerce – Disable Payment Methods Based on User Roles

Sometimes it happens that you need to disable Woocommerce payment methods based on user roles. For example, you want to hide Cash on Delivery method for logged-out users. Or you want to hide BACS method for all roles except customers and shop managers. And best of all – you can do it without any fancy plugin.

So, if this is something you are looking for then take a look at the tutorial I made you.

How to disable Woocommerce BACS Payment method for selected user roles?

In this first example I am going to hide Direct Bank Transfer (BACS) payment method for all user roles except customers, shop managers and administrators. Just grab this code here below and paste it to your theme’s functions.php file or better yer, use Code Snippets plugin for it. This way you will not lose all the modifications during your next theme switch.

// Enable BACS payment method for customers, shop managers and administrators

function bacs_payments( $available_payment_gateways ) {
$user = wp_get_current_user();
$allowed_roles = array('customer', 'administrator', 'shop_manager');
	if (!array_intersect($allowed_roles, $user->roles )) {
	if (isset($available_payment_gateways['bacs'])) {
		unset($available_payment_gateways['bacs']);
}
}
return $available_payment_gateways;
}
add_filter('woocommerce_available_payment_gateways', 'bacs_payments', 90, 1); 

Couple of things to point out:

  • ‘customer’, ‘administrator’, ‘shop_manager’ are the user roles I need the BACS to be active. Change them according to your needs
  • ‘bacs’ is the Payment method I need to be active for these user role. Change it according to your needs

How to find out Woocommerce payment method slugs?

Every payment method uses its own slugs. So, if you want to change the payment method name in the code and you don’t know the slug, then go to Woocommerce >> Settings >> Payment and open up a payment method. Now take a look at the URL on your browser. If it is https://yoursite.com/wp-admin/admin.php?page=wc-settings&tab=checkout&section=bacs then the bacs is the slug you need.

If the address is https://yoursite.com/wp-admin/admin.php?page=wc-settings&tab=checkout&section=paypal and you need to use the Paypal method, then last part of the URL (paypal) is the one you need.

How to disable Woocommerce Cash on Delivery payment method for selected user roles?

Next scenario: I want to disable Cash on delivery for logged-out users and subscriber roles. Therefore, I paste this snippet in my Code Snippets or functions.php file.

// Disable Cash on delivery for logged out users and subscriber roles

function disable_cod( $available_gateways ) {
    if ( isset($available_gateways['cod']) && (current_user_can('subscriber') || ! is_user_logged_in()) ) {
         unset($available_gateways['cod']);
     }
     return $available_gateways;
}
add_filter('woocommerce_available_payment_gateways', 'disable_cod', 99, 1);

Couple of things to point out:

  • ‘subscriber’ is the user role I need the COD to be disabled. Change it according to your needs
  • is_user_logged_in means “for logged-out users”.
  • ‘cod’ is the payment method I need to be disabled for the subscriber user role. Change it according to your needs

How to hide multiple Woocommerce Payment methods for selected user roles?

In this example I’m going to hide multiple Woocommerce payment methods (Paypal, Cheque and COD) for Shop Manager user role. If you want to do the same then use this code here below.

// Hide Paypal, Check payments and COD for shop manager role

add_filter('woocommerce_available_payment_gateways', 'shopmanager_payments', 1);
  function shopmanager_payments($gateways)
  {
      $current_user = wp_get_current_user();
      $role = $current_user->roles;
      global $woocommerce;
      /* add your user role in condition and payment method which you need to unset*/
      if ($role[0] == 'shop_manager') {
      	unset($gateways['paypal']);
	unset($gateways['cheque']);
	unset($gateways['cod']);
      }
      return $gateways;
  }

Couple of things to point out:

  • ‘shop_manager’ is the user role I need these payment mehtods to be disabled. Change it according to your needs
  • unset($gateways[‘paypal’]); – this disables Paypal payment method. Change it according to your needs
  • unset($gateways[‘cheque’]); – this disables Woocommerce Cheque payment method. Change it according to your needs
  • unset($gateways[‘cod’]); – this disables Woocommerce Cash on Delivery payment method. Change it according to your needs

Also, if you use your own custom payment methods then change the slug above. For example, if you use Stripe payment method and it has a slug “stripe” then add this line unset($gateways[‘stripe]);

Video: Woocommerce – Disable Payment Methods Based on User Roles

Useful Woocommerce tutorials

Here are some of my favorite Wordpress tools

Thank you for reading this article. I hope you found it helpful as you build your own websites and e-commerce sites. Here are some tools I use as a Wordpress developer and enthusiast that I hope you’ll also find helpful.

These are affiliate links, so if you do decide to use any of them, I’ll earn a commission and this helps me create these tutorials and make Youtube videos. But in all honesty, these are the exact tools that I use and recommend to everyone, even my friends and family.

Themes: For the last couple of years I have two go-to themes which I use for every kind of projects. Those two themes are Blocksy theme and Kadence Theme. On this site and my Youtube channel you’ll see a lot of tutorials I have made about them. If you would like to get a 10% discount for both of them then:

Code Snippets manager: WPCodeBox allows you to add code snippets to your site. Also, it allows you to build and manage your WordPress Code Snippets library in the cloud. You can grab it with the 20% discount here (SAVE 20% Coupon: WPSH20).

Contact forms: There are hundreds of contact forms out there but Fluent Forms is the one I like the most. If you need a 20% discount then use this link (save 20% coupon is WPSH20).

Gutenberg add-ons: If I need a good Gutenberg blocks add-on then Kadence Blocks is the one I have used the most. You’ll get 10% discount with the coupon SIMPLEHACKS here.

Website migration: While building a website you probably need a good plugin that can help you with the migration, backups, restoration and staging sites. Well, WpVivid is the one I have used for the last couple of years. If you use this link along with the WPSH20 coupon you’ll get 20% discount.

Woocommerce extensions: There are a bunch of Woocommerce extensions that I really like but the one that stands really out is Advanced Dynamic Pricing. Once again, you’ll get a 20% discount if you use this link here (save 20% coupon is WPSH20)

Web Hosting: If you would like to have a really fast and easy to use managed cloud hosting, then I recommend Verpex Hosting (see my review here). Btw, this site is hosted in Verpex.)

To see all my of most up-to-date recommendations, check out this resource that I made for you!

Do you want to thank me and buy me a beer?

Every donation is entirely welcome but NEVER required. Enjoy my work for free but if you would like to thank me and buy me a beer or two then you can use this form here below.

Donation Form (#2)

Best selling plugins

Janek T.
Janek T.

I am a Wordpress enthusiast who has been making websites since 2011. In this site I am offering simple to follow tips on how to use Wordpress and Woocommerce.
If you want to be the first to be notified about the new tutorials then please subscribe to my Youtube channel here
Follow me in Twitter here

Articles: 96