Disable Payment Gateway for Specific Country

In this tutorial we are going to talk about country based payments in WooCommerce. Because it is a common case that some payment gateways may not work for specific countries.

In this tutorial I will show you how to condition it easily and without any plugins.

Here is what we are going to create:

restrict WooCommerce payment gateways by country selected
At the moment of writing this tutorial PayPal doesn’t work for Monaco. So let’s deactivate PayPal payment gateway for Monaco.

But if you need custom coding help with conditioning WooCommerce payment gateways or maybe you need to develop one – please feel free to contact me.

add_filter( 'woocommerce_available_payment_gateways', 'rudr_gateway_by_country' );

function rudr_gateway_by_country( $gateways ) {
	
	if( is_admin() ) {
		return $gateways;
	}
	
	if( is_wc_endpoint_url( 'order-pay' ) ) { // Pay for order page

		$order = wc_get_order( wc_get_order_id_by_order_key( $_GET[ 'key' ] ) );
		$country = $order->get_billing_country();

	} else { // Cart page

		$country = WC()->customer->get_billing_country();

	}

	if ( 'MC' === $country ) {
		if ( isset( $gateways[ 'paypal' ] ) ) {
			unset( $gateways[ 'paypal' ] );
		}
	}

	return $gateways;

}

How to Check Multiple Countries

Well, it is quite rare when a payment gateway may not support the only one single country 😈 In many cases you would need to check against multiple countries.

if ( 'MC' === $country || 'MY' === $country ) {

Or even better way

$countries = array( // array of unsupported countries
	'MC',
	'MY',
   // more countries here...
);

if( in_array( $country, $countries ) ) {
Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.

Need some developer help? Contact me

Follow me on Twitter