Search the site:

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):

disable payment gateways for some countries in WooCommerce - PayPal disabled for India on the Checkout page

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.

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

List of Country Codes:

CodeLocation/Country
A1
Anonymous Proxy
A2Satellite Provider
O1Other Country
ADAndorra
AE
United Arab Emirates
AFAfghanistan
AG
Antigua and Barbuda
AIAnguilla
ALAlbania
AMArmenia
AOAngola
AP
Asia/Pacific Region
AQAntarctica
ARArgentina
AS
American Samoa
ATAustria
AUAustralia
AWAruba
AXAland Islands
AZAzerbaijan
BA
Bosnia and Herzegovina
BBBarbados
BDBangladesh
BEBelgium
BFBurkina Faso
BGBulgaria
BHBahrain
BIBurundi
BJBenin
BL
Saint Barthelemey
BMBermuda
BN
Brunei Darussalam
BOBolivia
BQ
Bonaire, Saint Eustatius and Saba
BRBrazil
BSBahamas
BTBhutan
BVBouvet Island
BWBotswana
BYBelarus
BZBelize
CACanada
CC
Cocos (Keeling) Islands
CD
Congo, The Democratic Republic of the
CF
Central African Republic
CGCongo
CHSwitzerland
CICote d’Ivoire
CKCook Islands
CLChile
CMCameroon
CNChina
COColombia
CRCosta Rica
CUCuba
CVCape Verde
CWCuracao
CXChristmas Island
CYCyprus
CZCzech Republic
DEGermany
DJDjibouti
DKDenmark
DMDominica
DO
Dominican Republic
DZAlgeria
ECEcuador
EEEstonia
EGEgypt
EHWestern Sahara
EREritrea
ESSpain
ETEthiopia
EUEurope
FIFinland
FJFiji
FK
Falkland Islands (Malvinas)
FM
Micronesia, Federated States of
FOFaroe Islands
FRFrance
GAGabon
GBUnited Kingdom
GDGrenada
GEGeorgia
GFFrench Guiana
GGGuernsey
GHGhana
GIGibraltar
GLGreenland
GMGambia
GNGuinea
GPGuadeloupe
GQ
Equatorial Guinea
GRGreece
GS
South Georgia and the South Sandwich Islands
GTGuatemala
GUGuam
GWGuinea-Bissau
GYGuyana
HKHong Kong
HM
Heard Island and McDonald Islands
HNHonduras
HRCroatia
HTHaiti
HUHungary
IDIndonesia
IEIreland
ILIsrael
IMIsle of Man
INIndia
IO
British Indian Ocean Territory
IQIraq
IR
Iran, Islamic Republic of
ISIceland
ITItaly
JEJersey
JMJamaica
JOJordan
JPJapan
KEKenya
KGKyrgyzstan
KHCambodia
KIKiribati
KMComoros
KN
Saint Kitts and Nevis
KP
Korea, Democratic People’s Republic of
KR
Korea, Republic of
KWKuwait
KYCayman Islands
KZKazakhstan
LA
Lao People’s Democratic Republic
LBLebanon
LCSaint Lucia
LILiechtenstein
LKSri Lanka
LRLiberia
LSLesotho
LTLithuania
LULuxembourg
LVLatvia
LY
Libyan Arab Jamahiriya
MAMorocco
MCMonaco
MD
Moldova, Republic of
MEMontenegro
MFSaint Martin
MGMadagascar
MHMarshall Islands
MKMacedonia
MLMali
MMMyanmar
MNMongolia
MOMacao
MP
Northern Mariana Islands
MQMartinique
MRMauritania
MSMontserrat
MTMalta
MUMauritius
MVMaldives
MWMalawi
MXMexico
MYMalaysia
MZMozambique
NANamibia
NCNew Caledonia
NENiger
NFNorfolk Island
NGNigeria
NINicaragua
NLNetherlands
NONorway
NPNepal
NRNauru
NUNiue
NZNew Zealand
OMOman
PAPanama
PEPeru
PF
French Polynesia
PG
Papua New Guinea
PHPhilippines
PKPakistan
PLPoland
PM
Saint Pierre and Miquelon
PNPitcairn
PRPuerto Rico
PS
Palestinian Territory
PTPortugal
PWPalau
PYParaguay
QAQatar
REReunion
RORomania
RSSerbia
RU
Russian Federation
RWRwanda
SASaudi Arabia
SBSolomon Islands
SCSeychelles
SDSudan
SESweden
SGSingapore
SHSaint Helena
SISlovenia
SJ
Svalbard and Jan Mayen
SKSlovakia
SLSierra Leone
SMSan Marino
SNSenegal
SOSomalia
SRSuriname
SSSouth Sudan
ST
Sao Tome and Principe
SVEl Salvador
SXSint Maarten
SY
Syrian Arab Republic
SZSwaziland
TC
Turks and Caicos Islands
TDChad
TF
French Southern Territories
TGTogo
THThailand
TJTajikistan
TKTokelau
TLTimor-Leste
TMTurkmenistan
TNTunisia
TOTonga
TRTurkey
TT
Trinidad and Tobago
TVTuvalu
TWTaiwan
TZ
Tanzania, United Republic of
UAUkraine
UGUganda
UM
United States Minor Outlying Islands
USUnited States
UYUruguay
UZUzbekistan
VA
Holy See (Vatican City State)
VC
Saint Vincent and the Grenadines
VEVenezuela
VG
Virgin Islands, British
VI
Virgin Islands, U.S.
VNVietnam
VUVanuatu
WF
Wallis and Futuna
WSSamoa
YEYemen
YTMayotte
ZASouth Africa
ZMZambia
ZWZimbabwe

 

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest
Inline Feedbacks
View all comments
Anthony Topham
Anthony Topham
1 year ago

Hi There,

If I was looking to enable a payment gateway for a single country or group of countries, would I do the following?

if ( isset( $available_payment_gateways[‘bacs’] ) && WC()->customer->get_billing_country() !== ‘US’ ) {
unset( $available_payment_gateways[‘bacs’] );

Thank you in advance.

Ony
Ony
12 days ago

Hello, I am looking for a code to make a specific payment option available to certain countries, say PayPal should only work for us, Canada, and UK?

Can you help?

from purchased

Subscribe for more offers

Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins and 50% OFF on Bundles
2
0
Would love your thoughts, please comment.x
()
x