Home » WooCommerce: Disable Payment Gateway for Specific User Role

WooCommerce: Disable Payment Gateway for Specific User Role

by Tutor Aspire

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: shop_manager” or enable a specific gateway for “user role: customer”. All you need is pasting the following code in your functions.php

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

/**
 * @snippet       Disable Payment Gateway for a Specific User Role | WooCommerce
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @testedwith    WooCommerce 3.8
 * @donate $9     https://www.tutoraspire.com
 */
 
add_filter( 'woocommerce_available_payment_gateways', 'tutoraspire_paypal_disable_manager' );
 
function tutoraspire_paypal_disable_manager( $available_gateways ) {
   if ( isset( $available_gateways['paypal'] ) && current_user_can( 'manage_woocommerce' ) ) {
      unset( $available_gateways['paypal'] );
   } 
   return $available_gateways;
}

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

/**
 * @snippet       Enable Payment Gateway for a Specific User Role | WooCommerce
 * @how-to        Get tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 3.8
 * @donate $9     https://www.tutoraspire.com
 */
 
add_filter( 'woocommerce_available_payment_gateways', 'tutoraspire_paypal_enable_manager' );
 
function tutoraspire_paypal_enable_manager( $available_gateways ) {
   if ( isset( $available_gateways['paypal'] ) && ! current_user_can( 'manage_woocommerce' ) ) {
      unset( $available_gateways['paypal'] );
   } 
   return $available_gateways;
}

How to Find WooCommerce Payment Gateway ID

Here’s the screenshot 🙂

Find Gateway ID (WooCommerce Payments)
Find Gateway ID (WooCommerce Payments)

Is There a 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 🙂

You may also like