If you need to block any requests based off certain IP Geolocation, this script below can help you do so

Tutorial

From your store admin, go to Online Store > Preferences
Scroll to the Additional scripts > Head section and paste this scripts.

Block 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>


Allow 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>


Replace the blockedCountries value as desired

Use the two letter country codes in ISO 3166-1 Alpha 2 format here

Save and you are done.
Was this article helpful?
Cancel
Thank you!