Block, allow certain countries from accessing your website
This section guides you on how to block or allow certain countries to access your online store.
From your store admin dashboard, go to Online Store > Design > Customize.
In General tab, Inside <head> tag section, copy and paste the code in the text box.
From your store admin, go to Online Store > Preferences.
In Additional scripts, Head section, copy and paste the below code in the text box:
Code to block customers from specific countries
Code to allow customers from specific countries
You can add more countries you want to block in blockedCountries, or add more countries you want to allow in allowedCountries in the code.
Click Save once finished.
Use the two letter country codes in ISO 3166-1 Alpha 2 format here.
Overview of Preferences
Block traffic from spy tools to your website
Step by Step Tutorial
If Website Builder is used
From your store admin dashboard, go to Online Store > Design > Customize.
In General tab, Inside <head> tag section, copy and paste the code in the text box.
If Theme Editor is used
From your store admin, go to Online Store > Preferences.
In Additional scripts, Head section, copy and paste the below code in the text box:
Code to block, allow certain countries from accessing your website
Code to block customers from specific countries
<script>
(function() {
// Update blocked countries list here
var blockedCountries = {'CN': true, 'VN': true, 'US': false};
var xhr = new XMLHttpRequest();
var redirect = 'https://www.google.com/not-found';
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
var location = JSON.parse(xhr.responseText);
if (location.result && location.result.country_code in blockedCountries && blockedCountries[location.result.country_code]) {
window.location.href = redirect;
}
}
};
if (window.location.href.indexOf(redirect) === -1) {
xhr.open('GET', 'https://' + window.location.host + '/api/catalog/location-lookup.json');
xhr.send();
}
})();
</script>
Code to allow customers from specific countries
<script>
(function() {
// Update allowed countries list here
var allowedCountries = {'US': true, 'CA': true, 'UK': true, 'CN': false};
var xhr = new XMLHttpRequest();
var redirect = 'https://www.google.com/not-found';
xhr.onreadystatechange = function() {
if (xhr.readyState === 4) {
var location = JSON.parse(xhr.responseText);
var allow = false;
if (!location.result) {
allow = true;
} else if (location.result.country_code in allowedCountries && allowedCountries[location.result.country_code]) {
allow = true;
}
// Redirect if not allowed
if (!allow) {
window.location.href = redirect;
}
}
};
if (window.location.href.indexOf(redirect) === -1) {
xhr.open('GET', 'https://' + window.location.host + '/api/catalog/location-lookup.json');
xhr.send();
}
})();
</script>
You can add more countries you want to block in blockedCountries, or add more countries you want to allow in allowedCountries in the code.
Click Save once finished.
Use the two letter country codes in ISO 3166-1 Alpha 2 format here.
Related articles
Overview of Preferences
Block traffic from spy tools to your website
Updated on: 19/02/2024
Thank you!