Disable Payment Methods for Specific Countries

In this tutorial, we will discuss country-based payments in WooCommerce.

It is a common case that some payment processors work only for customers from specific countries. For example, if you accept payments from customers from Sweden, you’d like to include Klarna payments for them; however, if you also have customers from all over the world, maybe it would be a great idea to hide the Klarna payment method from your WooCommerce store’s checkout page for some countries, right?

Below, I’m going to show you two simple ways of achieving that: at first, we will do it using my Simple Conditional Shipping and Payments plugin, and after that, I will also uncover a programmatic approach, using the woocommerce_available_payment_gateways filter hook.

Sounds good? Let’s dive into it now.

Country-Based Payments for WooCommerce with a Plugin

First of all, let’s take a look at my plugin, which allows you to enable or disable some payment gateways (methods) for selected countries.

Below, in the screenshot, you can see how I configured two conditions with the plugin:

The plugin settings can be found at WooCommerce > Settings > Conditions:

Configure WooCommerce payment methods by country
You could also notice from this screenshot that the plugin allows you to restrict multiple payment methods and select multiple countries.

Setting Up Payment Methods by Country Programmatically

It is time to talk about a programmatic approach, and as an example, let’s choose another payment gateway (let’s say, PayPal) and another unsupported country.

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 often that a payment processor doesn’t support multiple countries. In that case, it means that we need to slightly modify our condition in the code snippet above, for example:

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

Or an even better way:

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

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

Do you have any questions about the code or the plugin? Feel free to ask in the comments.

Misha Rudrastyh

Misha Rudrastyh

Hey guys and welcome to my website. For more than 15 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 X