Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to disable payment gateways for some user roles in WooCommerce

As a WooCommerce store owner, on many occasions, you may have come across a need to disable certain payment gateways. For instance, you may want to disable the Cash on Delivery option for all your customers i.e. logged in users having the role as “customer” and those users who are not logged in. Another instance may be that you want to disable online payment gateways for all your employees or fellow store administrators. Or you may want to disable just the “PayPal” option for reasons as simple as it not being widely used in your country. In this post, we will explore how you can disable payment gateways for some user roles in WooCommerce.

By default, you are usually able to view four payment gateways on the Checkout page viz. Direct Bank Transfer, Check payments, Cash on Delivery and PayPal:

disable payment gateways for some user roles in WooCommerce - Default available payment gateways

Now, let’s take an instance where you want to disable the Cash on Delivery option for all your customers, whether they are logged in or not. In case the user is a logged-in/registered user, you will have to check if their role is ‘customer’ because a logged-in user can also be an administrator or a contributor of the site. You can do this using either code snippets or plugins.  We will first do this using a code snippet. The below code snippet, upon being inserted in the functions.php file of your child theme will disable the Cash-on-Delivery option for your customers:

function ts_disable_cod( $available_payment_gateways ) { 

//Check whether the available payment gateways have the Cash on delivery option and if the user is not logged in or has the role customer 

	$user = wp_get_current_user();
        $allowed_roles = array('customer');
	if ( isset($available_payment_gateways['cod']) && (array_intersect($allowed_roles, $user->roles ) || !is_user_logged_in()) ) { 

		//Remove the cash on delivery payment gateway 
		unset($available_payment_gateways['cod']); 
	} 

	return $available_payment_gateways; 
} 

add_filter('woocommerce_available_payment_gateways', 'ts_disable_cod', 90, 1);

If you now log out and test the Checkout page, you will see that you will no longer be able to view the Cash on Delivery option:

disable payment gateways for some user roles in WooCommerce - Cash on Delivery option disabled for customers

You may also test the same by signing in as a Customer.

In the above example, we checked for the “Cash on Delivery” option. For other payment gateways, in place of “cod”, you would use the following keywords in the arguments:

Direct Bank Transfer: bacs
Check Payment: cheque
PayPal:
paypal

Let’s consider an example where you want to disable the PayPal, Direct Bank Transfer and Check Payment options for your employees so that they can checkout using the Cash on Delivery option alone. Employees usually will have an account on the site with their role being anything other than “customer” and “subscriber”. The code snippet, in this case, will look like below:

function ts_disable_all_but_cod( $available_payment_gateways ) {

    //Check whether the available payment gateways have the Cash on delivery option and if the user is not logged in or has the role customer
  $user = wp_get_current_user();
  $allowed_roles = array('customer','subscriber');
  
    if (!array_intersect($allowed_roles, $user->roles )) {		
    
    if (isset($available_payment_gateways['bacs'])) {
         unset($available_payment_gateways['bacs']);
      }
    if (isset($available_payment_gateways['cheque'])) {
         unset($available_payment_gateways['cheque']);
      }
    if (isset($available_payment_gateways['paypal'])) {
         unset($available_payment_gateways['paypal']);
      }		
 }
  
     return $available_payment_gateways;
}

add_filter('woocommerce_available_payment_gateways', 'ts_disable_all_but_cod', 90, 1);

If you now visit the Checkout page (as you’re an administrator), you will see the following:

disable payment gateways for some user roles in WooCommerce - All payment gateways except Cash on Delivery disabled for employees

The different user roles have different slugs or keywords as follows:

Shop Manager: shop_manager
Customer: customer
Subscriber: subscriber
Contributor: contributor
Author: author
Editor: editor
Administrator: administrator

The function wp_get_current_user() returns the current user object. You can store it in a variable such as “$user” as done in the code snippet above. $user->role will return the role of the current user. You can pass any of the above slugs (corresponding to different user roles) in an array as done in the code snippet above, and check for whether or not the current user role (returned by $user->role) exists in the array, as done above using the array_intersect() function.

In this way, we can use simple code snippets to disable any payment gateways depending on user roles. If you wish to have user roles that are different from the default ones mentioned above, you can use plugins such as the User Role Editor plugin. This is a free plugin which also has a paid version for additional, advanced features.

There are also plugins available for disabling payment gateways depending on user roles:

Payment Gateways by User Roles for WooCommerce is a free plugin that lets you include and exclude users from the available, default payment gateways. If you however wish to use other payment gateways, you will need to use their Pro version for the same.

WooCommerce Role Based Payment Shipping Methods: This is a paid plugin that lets you customise shipping methods along with payment gateways based on the role of the user.

This is how you can disable payment gateways using either code snippets or plugins. Sometimes you may also want to disable them based on the country of the customer. To know how you can disable payment gateways for some countries, refer to the this post.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x