How to Block Specific Countries from Accessing Your Shopify Store Without App
Once launched, a Shopify store is available worldwide. But in many cases, it’s necessary to control your site access, especially when you want to stop scammers and hackers from particular countries. There are many best Shopify apps available to help you block visitors from specific countries. But if you want to do it manually then you can check out this article.
Please follow the steps below:
- From your Shopify admin, go to Online Store > Themes.
- Find the theme you want to edit, and then click Actions > Edit code.
- Under Layout, select theme.liquid.
- Putting the script below top of this file1234567891011121314<script type="text/javascript">//Coded by [email protected] - HuraTips.ComHuraScriptCountryBlocker = function ($) {var appendCSS='';appendCSS+='<style>';appendCSS+='.hura-popup-modal{display:none;position:fixed;z-index:9999999;left:0;top:0;width:100%;height:100%;overflow:auto;background-color:rgba(0,0,0,1)}';appendCSS+='.hura-popup-modal .hura-popup-modal-content{background-color:#fefefe;margin:15% auto;padding:20px;border:1px solid #888;max-width:470px;position:relative}';appendCSS+='.hura-popup-modal .hura-popup-close{color:#aaa;float:right;font-size:28px;font-weight:700}';appendCSS+='.hura-popup-modal .hura-popup-close:hover,.hura-popup-close:focus{color:#000;text-decoration:none;cursor:pointer}';appendCSS+='.hura-popup-modal .hura-popup-title{text-align:center}';appendCSS+='.hura-popup-modal .hura-popup-subtitle{text-align:center}';appendCSS+='.hura-popup-modal .hura-popup-confirm-btn{width:100%;text-align:center}';appendCSS+='.hura-popup-modal .hura-popup-confirm-btn button{width:40%;padding:.4em .6em;border:none;border-radius:.25em;text-decoration:none;text-transform:uppercase;text-rendering:optimizeLegibility;background:#000;color:#fff;cursor:pointer}';appendCSS+='.hura-popup-modal .hura-credit{position:absolute;bottom:-40px;right:0;font-size:11px;margin: 0 0 20px 0;color: rgb(255, 255, 255);font-style:italic;}';appendCSS+='.hura-popup-modal .hura-credit a{color: rgb(255, 228, 0);}';appendCSS+='body {height: 100%;overflow-y: hidden;}';appendCSS+='</style>';var appendHTML='';appendHTML+='<div id="hura-country-blocker-popup" class="hura-popup-modal">';appendHTML+='<div class="hura-popup-modal-content">';appendHTML+='<div class="hura-popup-form-wapper">';appendHTML+='<h2 class="hura-popup-title">Not Authorized</h2>';appendHTML+='<p class="hura-popup-subtitle">Access denied! You are not authorized to access this site.</p>';appendHTML+='</div>';appendHTML+='<p class="hura-credit">Powered by <a href="//www.huratips.com" target="_blank">HuraTips.Com</a></p>';appendHTML+='</div>';appendHTML+='</div>'$.get('https://www.cloudflare.com/cdn-cgi/trace').done(function (data) {var country_code=data.replace(/(\r\n|\n|\r)/gm,"").split("loc=");country_code=(country_code=country_code[1].split("tls="))[0];var blocked_countries = "VN,CN"; // Change this value to Block Specific Countriesif(blocked_countries=blocked_countries.split(","),blocked_countries.includes(country_code)){try{window.stop()}catch(o){document.execCommand("Stop")}$("head").append(appendCSS),$("body").append(appendHTML),$("#hura-country-blocker-popup").show()}});}var jqHuraScript=document.createElement("script");jqHuraScript.setAttribute("src","https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"),jqHuraScript.setAttribute("type","text/javascript"),jqHuraScript.readyState?jqHuraScript.onreadystatechange=function(){"loaded"!=this.readyState&&"complete"!=this.readyState||HuraScriptCountryBlocker(jQuery.noConflict(!0))}:jqHuraScript.onload=function(){HuraScriptCountryBlocker(jQuery.noConflict(!0))};var d=document.getElementsByTagName("head")[0]||document.documentElement;d.appendChild(jqHuraScript);</script>
- Click Save and you are done.
You can use the tool here to generate code with custom texts and select block-specific countries.
I hope it’s helpful for you.
Happy coding!
this code blocked not only the countries we have chosen but all countries….
Hmm! This script only allows blocking a country. If you want to block multiple countries, I think you should try a Shopify app here: https://apps.shopify.com/prevent-content-theft?ref=hura.
You can refer to the guide here: https://www.huratips.com/tech-tips/how-to-prevent-visitors-from-certain-countries-from-entering-your-shopify-store-using-anticopy-from-hura-apps.html
So it only allows blocking one country? Will it work if the visitors will be using VPN?
Yes, it’s working with a VPN.
If you have coding knowledge, you can adjust this code to working multiple countries.
Hey there, I had a couple of the issues above, but I found out that the script only allows for one instance of a country or for some reason does not work. When I try to put a string of them together, it stops working. Also if I try to add separate if/else statements no go. I tried copying the entire scripts and making separate instances, still nothing. Right now I can only block one country with this script. Do you have any idea why this would be happening?
dude!! never mind I got the app. So easy and thank you so much for making it affordable!!! So much appreciation for keeping the online community helpful 🙂
Hey Kiet First of all, thank you for sharing, but I have a problem now, my jqHuraTipsScript.readyState shows that it is undefined, and the result is that all codes cannot work normally
You should use the tool here to generate new code: https://www.huratips.com/tools/index.php?page=country-blocker