Menu Close

WooCommerce: Disable Payment Gateway for Specific User Role

default

You may want to disable payment gateways depending on the logged in user role. For example, you may want to disable PayPal for user role “subscriber” or enable a specific gateway for user role “customer”.

All you need is to paste the following code in your functions.php or to install a super simple plugin. Enjoy!

The default WooCommerce settings may not be enough for you. What if you want “customers” to only pay via PayPal, while you want “subscribers” to be able to pay via bank transfers as well? Here’s the fix!

PHP Snippet 1: Disable Payment Gateway for a Specific User Role @ WooCommerce Checkout

/**
 * @snippet       Disable Payment Gateway by User Role | WooCommerce
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 7
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_paypal_disable_manager' );
 
function bbloomer_paypal_disable_manager( $available_gateways ) {
   if ( isset( $available_gateways['paypal'] ) && wc_current_user_has_role( 'customer' ) ) {
      unset( $available_gateways['paypal'] );
   } 
   return $available_gateways;
}

PHP Snippet 2: Enable Payment Gateway Only for a Specific User Role @ WooCommerce Checkout

We will basically hide the gateway for all the other roles.

/**
 * @snippet       Enable Payment Gateway for User Role | WooCommerce
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 7
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
 
add_filter( 'woocommerce_available_payment_gateways', 'bbloomer_paypal_enable_manager' );
 
function bbloomer_paypal_enable_manager( $available_gateways ) {
   if ( isset( $available_gateways['paypal'] ) && ! wc_current_user_has_role( 'subscriber' ) ) {
      unset( $available_gateways['paypal'] );
   } 
   return $available_gateways;
}

How to Find WooCommerce Payment Gateway ID

Here’s the screenshot. Basically, go to WooCommerce Settings > Payments, and either:

  • hover onto the “Manage” button, and read the URL parameter “section“. In this case, you will see “&section=bacs” so “bacs” is the payment gateway ID
  • or right click on the Method title, click on Inspect (Google Chrome), and find the “data-gateway_id” attribute. In this case, you will see “tr data-gateway_id=bacs” so “bacs” is the payment gateway ID

Mini-Plugin: Business Bloomer WooCommerce Toggle Payments By User Role

You don’t feel confident with coding? You need more control over your payment/user role exclusions? You don’t want to purchase yet another bloated, expensive plugin? Great!

Business Bloomer WooCommerce Toggle Payments By User Role is a mini WooCommerce plugin, without the usual hassles. One feature. Lifetime license. No annoying subscriptions. 1 plugin file. A few lines of code. No banners. No up-sells. No WP notifications. Use it on as many websites as you like. Lifetime support. 1-page documentation. A single and easy admin dashboard.

Screenshot of the settings:

Quick demo:

As you can see the settings are pretty straight forward. Select a payment method you wish to hide/show from the left, and the user role that should trigger that from the right. Add more rules if needed. Simple!

Advanced WooCommerce “Payment Gateways by User Role” Plugin

If you don’t feel 100% confident with coding, I decided to look for a reliable plugin that achieves the same result of this snippet (and more).

In this case, I found the WooCommerce Conditional Payment Gateways plugin 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.

But in case you don’t want to use plugins and wish to code (or wish to try that), then keep reading 🙂

View Source
Posted in WooCommerce Tips