In today’s global marketplace, it’s common for online businesses to operate across multiple countries. However, there may be situations where you want to restrict payment options for customers in certain countries in WooCommerce. This could be due to various reasons, such as legal regulations, currency conversion issues, or high fraud rates.
Disabling payment gateways for specific countries can be a daunting task for businesses, especially when they have to manage multiple payment gateways.
Also Read: How To Disable Payment Gateways For Specific User Roles in WooCommerce
In this blog, I will guide you through the process of disabling payment gateways for specific countries programmatically in woocommerce.
So, whether you’re a small online store or a large e-commerce platform, this blog will provide you with the necessary information to disable payment gateways for specific countries in woocommerce in a few simple steps. Let’s get started!
Navigate to your website’s child theme’s root and search for the functions.php file. Scroll to the bottom and paste the Code given below.
<?php
add_filter( 'woocommerce_available_payment_gateways', 'cxc_disable_payment_gateway_by_country' );
function cxc_disable_payment_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 ( 'CA' === $country ) {
if ( isset( $gateways[ 'cod' ] ) ) {
unset( $gateways[ 'cod' ] );
}
}
return $gateways;
}
?>
Here is the country code based on the location
Country Code | Location/Country |
AF | Afghanistan |
AX | Åland Islands |
AL | Albania |
DZ | Algeria |
AS | American Samoa |
AD | Andorra |
AO | Angola |
AI | Anguilla |
AQ | Antarctica |
AG | Antigua and Barbuda |
AR | Argentina |
AM | Armenia |
AW | Aruba |
AU | Australia |
AT | Austria |
AZ | Azerbaijan |
BS | Bahamas |
BH | Bahrain |
BD | Bangladesh |
BB | Barbados |
BY | Belarus |
PW | Belau |
BE | Belgium |
BZ | Belize |
BJ | Benin |
BM | Bermuda |
BT | Bhutan |
BO | Bolivia |
BQ | Bonaire, Saint Eustatius and Saba |
BA | Bosnia and Herzegovina |
BW | Botswana |
BV | Bouvet Island |
BR | Brazil |
IO | British Indian Ocean Territory |
BN | Brunei |
BG | Bulgaria |
BF | Burkina Faso |
BI | Burundi |
KH | Cambodia |
CM | Cameroon |
CA | Canada |
CV | Cape Verde |
KY | Cayman Islands |
CF | Central African Republic |
TD | Chad |
CL | Chile |
CN | China |
CX | Christmas Island |
CC | Cocos (Keeling) Islands |
CO | Colombia |
KM | Comoros |
CG | Congo (Brazzaville) |
CD | Congo (Kinshasa) |
CK | Cook Islands |
CR | Costa Rica |
HR | Croatia |
CU | Cuba |
CW | Curaçao |
CY | Cyprus |
CZ | Czech Republic |
DK | Denmark |
DJ | Djibouti |
DM | Dominica |
DO | Dominican Republic |
EC | Ecuador |
EG | Egypt |
SV | El Salvador |
GQ | Equatorial Guinea |
ER | Eritrea |
EE | Estonia |
SZ | Eswatini |
ET | Ethiopia |
FK | Falkland Islands |
FO | Faroe Islands |
FJ | Fiji |
FI | Finland |
FR | France |
GF | French Guiana |
PF | French Polynesia |
TF | French Southern Territories |
GA | Gabon |
GM | Gambia |
GE | Georgia |
DE | Germany |
GH | Ghana |
GI | Gibraltar |
GR | Greece |
GL | Greenland |
GD | Grenada |
GP | Guadeloupe |
GU | Guam |
GT | Guatemala |
GG | Guernsey |
GN | Guinea |
GW | Guinea-Bissau |
GY | Guyana |
HT | Haiti |
HM | Heard Island and McDonald Islands |
HN | Honduras |
HK | Hong Kong |
HU | Hungary |
IS | Iceland |
IN | India |
ID | Indonesia |
IR | Iran |
IQ | Iraq |
IE | Ireland |
IM | Isle of Man |
IL | Israel |
IT | Italy |
CI | Ivory Coast |
JM | Jamaica |
JP | Japan |
JE | Jersey |
JO | Jordan |
KZ | Kazakhstan |
KE | Kenya |
KI | Kiribati |
KW | Kuwait |
KG | Kyrgyzstan |
LA | Laos |
LV | Latvia |
LB | Lebanon |
LS | Lesotho |
LR | Liberia |
LY | Libya |
LI | Liechtenstein |
LT | Lithuania |
LU | Luxembourg |
MO | Macao |
MG | Madagascar |
MW | Malawi |
MY | Malaysia |
MV | Maldives |
ML | Mali |
MT | Malta |
MH | Marshall Islands |
MQ | Martinique |
MR | Mauritania |
MU | Mauritius |
YT | Mayotte |
MX | Mexico |
FM | Micronesia |
MD | Moldova |
MC | Monaco |
MN | Mongolia |
ME | Montenegro |
MS | Montserrat |
MA | Morocco |
MZ | Mozambique |
MM | Myanmar |
NA | Namibia |
NR | Nauru |
NP | Nepal |
NL | Netherlands |
NC | New Caledonia |
NZ | New Zealand |
NI | Nicaragua |
NE | Niger |
NG | Nigeria |
NU | Niue |
NF | Norfolk Island |
KP | North Korea |
MK | North Macedonia |
MP | Northern Mariana Islands |
NO | Norway |
OM | Oman |
PK | Pakistan |
PS | Palestinian Territory |
PA | Panama |
PG | Papua New Guinea |
PY | Paraguay |
PE | Peru |
PH | Philippines |
PN | Pitcairn |
PL | Poland |
PT | Portugal |
PR | Puerto Rico |
QA | Qatar |
RE | Reunion |
RO | Romania |
RU | Russia |
RW | Rwanda |
BL | Saint Barthélemy |
SH | Saint Helena |
KN | Saint Kitts and Nevis |
LC | Saint Lucia |
SX | Saint Martin (Dutch part) |
MF | Saint Martin (French part) |
PM | Saint Pierre and Miquelon |
VC | Saint Vincent and the Grenadines |
WS | Samoa |
SM | San Marino |
ST | São Tomé and Príncipe |
SA | Saudi Arabia |
SN | Senegal |
RS | Serbia |
SC | Seychelles |
SL | Sierra Leone |
SG | Singapore |
SK | Slovakia |
SI | Slovenia |
SB | Solomon Islands |
SO | Somalia |
ZA | South Africa |
GS | South Georgia/Sandwich Islands |
KR | South Korea |
SS | South Sudan |
ES | Spain |
LK | Sri Lanka |
SD | Sudan |
SR | Suriname |
SJ | Svalbard and Jan Mayen |
SE | Sweden |
CH | Switzerland |
SY | Syria |
TW | Taiwan |
TJ | Tajikistan |
TZ | Tanzania |
TH | Thailand |
TL | Timor-Leste |
TG | Togo |
TK | Tokelau |
TO | Tonga |
TT | Trinidad and Tobago |
TN | Tunisia |
TR | Turkey |
TM | Turkmenistan |
TC | Turks and Caicos Islands |
TV | Tuvalu |
UG | Uganda |
UA | Ukraine |
AE | United Arab Emirates |
GB | United Kingdom (UK) |
US | United States (US) |
UM | United States (US) Minor Outlying Islands |
UY | Uruguay |
UZ | Uzbekistan |
VU | Vanuatu |
VA | Vatican |
VE | Venezuela |
VN | Vietnam |
VG | Virgin Islands (British) |
VI | Virgin Islands (US) |
WF | Wallis and Futuna |
EH | Western Sahara |
YE | Yemen |
ZM | Zambia |
ZW | Zimbabwe |
Also Read:
How to Edit Payment Gateway Title and Description in WooCommerce
How to disable all payment methods in WooCommerce
How To Change Place Order Button Text on Checkout Page WooCommerce
Why would I want to disable payment gateways for specific countries?
There could be various reasons for disabling payment gateways for specific countries, such as legal restrictions, high fraud rates, currency conversion issues, or simply not having the resources to support transactions in certain countries.
Can I disable payment gateways for multiple countries at once?
Yes, you can disable payment gateways for multiple countries at once by using programmatic solutions such as APIs or plugins.
Is it legal to disable payment gateways in some countries?
Yes, it is legal to disable payment gateways for some countries if it complies with local laws and regulations.