Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

You must login to ask a question.

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

WebPro Care Latest Articles

How to disable payment gateways for some countries in WooCommerce

How to disable payment gateways for some countries in WooCommerce

In my last post, I talked about how you can disable payment gateways in WooCommerce based on different user roles. Sometimes, there are instances where you may need to do this based on the country of the customer. This can be for different reasons such as the payment gateway not being widely used in that country, or that you want to encourage your customers to use a certain payment gateway to make payments and so you would want to selectively enable that payment gateway. In any case, when it comes to an ecommerce store which serves on a global scale, this is a useful feature. Read on to find out how you can disable payment gateways for some countries in WooCommerce.

As with most things, you can do this using code snippets or plugins.

Using a code snippet to disable payment gateways for some countries

To start with, let’s consider an instance where you want to disable PayPal for India. Paste the following code in the functions.php file of your child theme:

add_filter( 'woocommerce_available_payment_gateways', 'ts_disable_payment_gateway_by_country' );

function ts_disable_payment_gateway_by_country( $available_payment_gateways ) {

if ( is_admin() ) return $available_payment_gateways;

if ( isset( $available_payment_gateways['paypal'] ) && WC()->customer->get_billing_country() == 'IN' ) {

unset( $available_payment_gateways['paypal'] );

}

return $available_payment_gateways;

}

Here, we have added a function to the hook woocommerce_available_payment_gateways, which checks the country code of the country we are writing the condition for, and disables PayPal for that country (India in our  case):

 

Every country has a different code just like we have used “IN” for India here. You can find the list of country codes at the end of this post.

The keywords/slugs for the default available payment gateways are:

PayPal: paypal
Direct Bank Transfer: bacs
Cash on Delivery: cod
Cheque: cheque

Let’s consider another instance where we want to have only the PayPal option enabled for customers in the US, with this option being disabled for those in India. In this case, we will adjust a few  more lines to our code snippet above so that it disables the other payment gateways for customers in the US:

add_filter( 'woocommerce_available_payment_gateways', 'ts_disable_payment_gateway_by_country' );

function ts_disable_payment_gateway_by_country( $available_payment_gateways ) {

if ( is_admin() ) return $available_payment_gateways;

if ( isset( $available_payment_gateways['paypal'] ) && WC()->customer->get_billing_country() == 'IN' ) {

unset( $available_payment_gateways['paypal'] );

}

if ( WC()->customer->get_billing_country() == 'US' ) {

if (isset($available_payment_gateways['bacs'])) { unset($available_payment_gateways['bacs']); }

if (isset($available_payment_gateways['cheque'])) { unset($available_payment_gateways['cheque']); }

if (isset($available_payment_gateways['cod'])) { unset($available_payment_gateways['cod']); }

}

return $available_payment_gateways;

}

In this way, you can disable payment gateways based on the country of the customer.

Using a plugin to disable payment gateways for some countries

There are also plugins available to disable payment gateways as per the country of the customer. One such plugin is the Country Based Payments plugin. It is a free plugin that you can use to decide which payment gateway should be available in which country. While the plugin interface is friendly and self-explanatory, if you want to exclude just one country from a payment gateway, then the way around it is to select all countries against that payment gateway, and then individually unselect the countries which you want to disable that payment gateway for.

Also, using the Conditional Payment Methods for WooCommerce plugin, you can hide/show any payment gateways (including custom gateways) based on countries – customers’ billing/shipping location, or product taxonomy or order total. For example, show PayPal and Stripe in US, Canada and European countries.

In this manner, you can use code snippets as well as plugins to disable payment gateways based on the country.

Leave a comment

1 Comment

  1. Sure, you can refer to the code snippet below for the same:
    function ts_enable_payment_gateway_for_countries($available_gateways) {
    // List of allowed countries for the payment gateway
    $allowed_countries = array(‘US’, ‘CA’, ‘GB’); // ISO 3166-1 alpha-2 country codes

    // Get the current customer’s country
    $customer_country = WC()->customer->get_billing_country();

    // Check if the customer’s country is in the allowed list
    if (in_array($customer_country, $allowed_countries)) {
    return $available_gateways; // Return all available gateways, including the payment gateway
    } else {
    // Remove the payment gateway from available gateways
    unset($available_gateways[‘paypal’]);
    return $available_gateways;
    }
    }

    add_filter(‘woocommerce_available_payment_gateways’, ‘ts_enable_payment_gateway_for_countries’);