WordPress / Web Development Tutorials
(Best WordPress Tutorials)

CSSHTMLJavaScriptjQueryMySQLPHPSilvaTechnologiesWooCommerceWordpress
Silva Web Designs - Blog

WooCommerce: Disable Payment Gateway for Specific User Role

You may want to disable payment gateways depending on the user role or user capability. For example, you may want to disable PayPal for ‘user role: subscriber’ or enable a specific gateway for another user role. This can be achieved without a plugin, all you need to do is paste the following code in your functions.php file.


/**
 * Disable Payment Gateway for a Specific User Role | WooCommerce
 */

add_filter( 'woocommerce_available_payment_gateways', 'payment_gateway_disable_private' );

 function payment_gateway_disable_private( $available_gateways ) {

    $user = wp_get_current_user();

    if ( isset( $available_gateways['cod'] ) && !is_user_logged_in() || isset( $available_gateways['cod'] ) && in_array('subscriber', $user->roles)  ) {
        unset( $available_gateways['cod'] );
    }

   return $available_gateways;

}

So what the above code will do is remove the ‘Cash On Delivery’ option if you are not logged in, or if you are a user with the ‘Subscriber’ role.

How to Find WooCommerce Payment Gateway ID

If you navigate to WooCommerce –> Settings –> Payments tab, if you inspect the element or view source of this page, you will see a list of codes within a table of all the gateways available on your website as shown in the below screenshot:

What if you want to disable payments for multiple user types?

Okay, we have you covered you here as well. Let’s say you want to disable ‘Stripe’ payment gateway for both the Administrator and Subscriber user but no other user…

What we need to use here is the PHP’s Array Intersect.

Again, this code will get added to your functions.php file and will look something like this:


/**
* Disable Payment Gateway for a Specific User Roles | WooCommerce
*/

function ts_disable_stripe( $available_payment_gateways ) {

//Check whether the available payment gateways have the Stripe option and if the user is not logged in or has the role administrator or subscriber
$user = wp_get_current_user();
$allowed_roles = array(‘administrator’, ‘subscriber’);

if ( isset($available_payment_gateways[‘stripe’]) && (array_intersect($allowed_roles, $user->roles ) || !is_user_logged_in()) ) {

//Remove the stripe payment gateway
unset($available_payment_gateways[‘stripe’]);
}

return $available_payment_gateways;
}

add_filter(‘woocommerce_available_payment_gateways’, ‘ts_disable_stripe’, 90, 1);

Now, the $allowed_roles can list an array of all the users you would like to not display a specific payment gateway for.

Is there a Plugin alternative?

If you don’t feel 100% confident with coding there is a plugin available which offers the same capabilities as the non-plugin method.

There is a plugin called WooCommerce Conditional Payment Gateways to be the most complete when you need to enable/disable payment gateways based on certain criteria. You can create unlimited ‘rules’ and use, for example, cart totals, billing country, shipping country, user role and much more to define which payment gateway shows and which not.

 

Nathan da Silva - Profile

Posted by: Nathan da Silva

Nathan is the Founder of Silva Web Designs. He is passionate about web development, website design and basically anything digital-related. His main expertise is with WordPress and various other CMS frameworks. If you need responsive design, SEO, speed optimisation or anything else in the world of digital, you can contact Silva Web Designs here; [email protected]

It’s good to share

7 thoughts on “WooCommerce: Disable Payment Gateway for Specific User Role

  1. What would be the syntax if one wanted to disable the payment method for more than 1 user role? i.e. Private customer and business customer.

    I’ve successfully disabled it for one of them at a time but not both.

    1. You could use: array_intersect.

      array_intersect will check between two array to see if the needle ($roles) exists in the haystack ($user_info->roles). I have tested this against my own and works well.

      See below the use of array_intersect.

      global $user_login, $current_user;
      get_currentuserinfo();
      $user_info = get_userdata($current_user->ID);
      $roles = array (
      ‘administrator’,
      ‘subscriber’,
      );

      if (is_user_logged_in() && array_intersect( $roles, $user_info->roles)) {

      echo ‘success’;

      } else {

      echo ‘failure’;

      }

  2. Using the code posted in this article or should I make up new code?
    If it needs to be added to the existing code, where would I put this?

    I’m a student so I’m not very well versed in PHP yet, but I’d like to understand how something like this works

    1. Okay so I’ve tested the below code and this works. Firstly, you can create an array of the roles you want to disable the payment gateway for. In the example below, I have disabled Stripe payments for the Administrator and Subscriber role. You can change ‘stripe’ to the payment gateway that you wish to disable, i.e. cod, for cash on delivery. This code gets added to your theme folders functions.php file. I’ll update the post to include this solution as it might help others. Let me know if you need any further help.

      /**
      * Disable Payment Gateway for a Specific User Roles | WooCommerce
      */

      function ts_disable_stripe( $available_payment_gateways ) {

      //Check whether the available payment gateways have the Stripe option and if the user is not logged in or has the role administrator or subscriber
      $user = wp_get_current_user();
      $allowed_roles = array(‘administrator’, ‘subscriber’);

      if ( isset($available_payment_gateways[‘stripe’]) && (array_intersect($allowed_roles, $user->roles ) || !is_user_logged_in()) ) {

      //Remove the stripe payment gateway
      unset($available_payment_gateways[‘stripe’]);
      }

      return $available_payment_gateways;
      }

      add_filter(‘woocommerce_available_payment_gateways’, ‘ts_disable_stripe’, 90, 1);

Join the discussion

Related Posts

Related - How to Completely Remove Comments From a WordPress Site

Wordpress / 22nd December 2020

How to Completely Remove Comments From a WordPress Site

Read More

WooCommerce / 2nd March 2020

Decrease or Disable the Strength Required for your WooCommerce Passwords

Read More