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

  • How to sell tickets with Woocommerce?

  • Kadence WooExtras Plugin – Full Overview

  • How to Automatically Delete Woocommerce Images After Deleting a Product?

  • 12 useful Woocommerce snippets & hacks

  • How to Show Woocommerce Variations as Single Products?

Leave a Comment

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

Scroll to Top