ShopBase Help Center

Explore How-To's and learn eCommerce best practices from our knowledge base.

阻止或允许某些国家/地区的访客访问商店

阻止或允许某些国家/地区的访客访问商店

本文也有以下语言版本:

本文将指导您添加代码以阻止或允许某些国家/地区的访客访问您的在线商店。

指南

在ShopBase后台中,转到 Online Store > Preferences
往下拉到 Additional scripts部分,然后将以下代码粘贴到 Head字段中:
用于阻止某些国家/地区的访客访问商店的代码
<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>
用于允许某些国家/地区的访客访问商店的代码
<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>
您可以在第 2 步代码的 blockedCountries中添加更多要阻止的国家/地区,或在 allowedCountries中添加更多要允许的国家/地区。
点击 Save以保存。

请使用 ISO 3166-1 Alpha 2 格式两个字母的国家代码。

为了确保安全并防止支付干扰,添加到店铺的脚本不会在结账页面加载。建议使用 ShopBase 内置的功能或对接方式。