Start a conversation

Hide all countries except your country from shipping estimate tool

This example, I want to hide all countries except 'United State', 'United Kingdom', and 'Australia' from the shipping estimate tool in the cart page. Let check the below script.


<script>
    (function () {
        var allowedCountries = [
            'Australia',
            'United Kingdom',
            'United States',
        ];

        var defaultCountry = 'United States';

        function removeCountry(countries, changeValue, trigger) {
            if (countries.length) {
                var dropdown = countries[0].parentNode;
                for (var i in countries) {
                    if (typeof countries[i] == 'object') {
                        if (allowedCountries.indexOf(countries[i].innerText.trim()) == -1) {
                            dropdown.removeChild(countries[i]);
                        }
                    }
                }

                if (changeValue) {
                    dropdown.selectedIndex = 0;
                    var options = dropdown.querySelectorAll('option');

                    for (var i in options) {
                        if (typeof options[i] == 'object') {
                            if (options[i].innerText.trim() == defaultCountry) {
                                dropdown.value = options[i].getAttribute('value');
                                dropdown.selectedIndex = i;
                            }
                        }
                    }
                }

                if (trigger) {
                    setTimeout(function () {
                        if (document.createEventObject) {
                            dropdown.fireEvent("onchange");
                        } else {
                            var evt = document.createEvent("HTMLEvents");
                            evt.initEvent("change", false, true);
                            dropdown.dispatchEvent(evt);
                        }
                    }, trigger);
                }
            }
        }

        removeCountry(document.querySelectorAll('.shipping-estimator #shipping-country option'), true, 1000);
    })();
</script>

Update your country list for the "countriesList" variable in the above script.


Where can I find my correct country name? That is a good question!

You can find your country name in the shipping estimate tool. Click on the country dropdown  list, you will see your country name


Then enter your script into your Footer Script (Manager panel > Storefront > Footer Script)

Choose files or drag and drop files
Was this article helpful?
Yes
No
  1. Papathemes

  2. Posted
  3. Updated

Comments