Disable Payment Gateways based on User Role

In this tutorial I will show you how to enable or disable WooCommerce payment gateways for a specific user role. And also how to condition them for registered users.

If you need custom coding help with WooCommerce payment gateways – contact me.

All the code below is based on woocommerce_available_payment_gateways filter hook and can be inserted to your current or child theme functions.php file or a custom plugin.

Disable Payment Methods for a Specific User Role

/**
 * @snippet       Disable WooCommerce Payment Gateway for a Specific User Role
 * @author        Misha Rudrastyh
 * @url           https://rudrastyh.com/woocommerce/disable-payment-gateway-by-user-role.html
 */
add_filter( 'woocommerce_available_payment_gateways', 'rudr_turn_off_cod' );
  
function rudr_turn_off_cod( $available_gateways ) {
	
	if( current_user_can( 'subscriber' ) ) {
		if ( isset( $available_gateways[ 'cod' ] ) ) {
			unset( $available_gateways[ 'cod' ] );
		}
		// if you need to disable multiple payment gateways just add similar code
		// if ( isset( $available_gateways[ 'payment_gateway_2' ] ) ) {
		//		unset( $available_gateways[ 'payment_gateway_2' ] );
		// }
	}

	return $available_gateways;
}

Enable Payment Methods for a Specific User Role

Code is very similar to a previous snippet, the difference is just one symbol! So I just copy the part of it.

if( ! current_user_can( 'subscriber' ) ) {

So we disable our payment gateways for any user role except Subscriber role.

Enable Payment Gateway for Registered Users

This code is also based on a snippet I showed you before, all you need to do is just to change one line of it.

if( ! is_user_logged_in() ) {

So the result is that we disable payment gateway for all users who is not logged in.

Misha Rudrastyh

Misha Rudrastyh

I develop websites since 2008, so it is total of 13 years of experience, oh my gosh. Most of all I love love love to create websites with WordPress and Gutenberg, some ideas and thoughts I share throughout my blog.

Need some developer help? Contact me