How To Disable Payment Gateways For Specific User Roles in WooCommerce

How-To-Disable-Payment-Gateways-For-Specific-User-Roles-in-WooCommerce
370 Views
0
(0)

I’ll teach you how to enable or disable WooCommerce payment gateways for a certain user role in this article. It is often necessary to disable WooCommerce payment methods based on user roles.

For instance, you could wish to conceal the Cash on Delivery method from logged-out users.

Disable Payment Gateways Methods for a Specific User Role

In this first example, I will conceal the Cash On Delivery (COD) payment mechanism from specific user roles. Simply copy the code below and paste it into your theme’s functions.php file, or better yet, yer

<?php
add_filter( 'woocommerce_available_payment_gateways', 'cxc_disable_cod_payment_gateway_function' );

function cxc_disable_cod_payment_gateway_function( $available_gateways ) {
	//Disable Payment Gateways Methods for a Specific User Role customer
	if( current_user_can( 'customer' ) ) {

		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[ 'cxc_payment_gateway_2' ] ) ) {
		//		unset( $available_gateways[ 'cxc_payment_gateway_2' ] );
		// }
	}

	return $available_gateways;
} 
?>

Enable Payment Gateways Methods for a Specific User Role

<?php
add_filter( 'woocommerce_available_payment_gateways', 'cxc_disable_cod_payment_gateway_function' );

function cxc_disable_cod_payment_gateway_function( $available_gateways ) {
	
	//Enable Payment Gateways Methods for a Specific User Role customer
	if( ! current_user_can( 'customer' ) ) {

		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[ 'cxc_payment_gateway_2' ] ) ) {
		//		unset( $available_gateways[ 'cxc_payment_gateway_2' ] );
		// }
	}

	return $available_gateways;
}
?>

Enable Payment Gateway Methods for Registered User

<?php
add_filter( 'woocommerce_available_payment_gateways', 'cxc_disable_cod_payment_gateway_user_not_logged_in', 999 );

function cxc_disable_cod_payment_gateway_user_not_logged_in( $available_gateways ) {
	
	if( ! is_user_logged_in() ) {

		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[ 'cxc_payment_gateway_2' ] ) ) {
		//		unset( $available_gateways[ 'cxc_payment_gateway_2' ] );
		// }

	}

	return $available_gateways;
} 
?>

If you believe this code saved you time and money, please subscribe to our blog post updates. Thank you so much 😉

Also Read:

How to Edit Payment Gateway Title and Description in WooCommerce

How to add content after or before the ‘add to cart’ button on single product page woocommerce

How useful was this blog?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this blog.

  • CodexCoach

    - Web Development Expert

    CodexCoach is a skilled tech educator known for easy-to-follow tutorials in coding, digital design, and software development. With practical tips and interactive examples, CodexCoach helps people learn new tech skills and advance their careers.

Leave a comment

Your email address will not be published. Required fields are marked *