Restrict country list

Could the Commerce country list be filtered by the allowed_country values of the Country module?

At the moment this would require changes to the frontend template.

For example, in your frontend/checkout/partial/billing-address-fields.twig and/or frontend/checkout/partial/shipping-address-fields.twig template change the generation of the countries dropdown from:

        <div class="c-field {% if error_billing_country %}error{% endif %}">
            <label for="address-billing-country">{{ lex('commerce.address.country') }}</label>
            <select name="address[billing][country]" id="address-billing-country">
                <option value=""></option>
                {% for country in countries %}
                    <option value="{{ country.iso }}" {% if address_billing_country == country.iso %}selected="selected"{% endif %}>{{ country.shortname }}</option>
                {% endfor %}
            </select>
            {% if error_billing_country %}<span class="c-field-error">{{ error_billing_country }}</span>{% endif %}
        </div>

to:

        <div class="c-field {% if error_billing_country %}error{% endif %}">
            <label for="address-billing-country">{{ lex('commerce.address.country') }}</label>
            <select name="address[billing][country]" id="address-billing-country">
                <option value=""></option>
                {% for country in countries %}
                    {% if country.iso in ["NL", "DE", "FR"] %}
                        <option value="{{ country.iso }}" {% if address_billing_country == country.iso %}selected="selected"{% endif %}>{{ country.shortname }}</option>
                    {% endif %}
                {% endfor %}
            </select>
            {% if error_billing_country %}<span class="c-field-error">{{ error_billing_country }}</span>{% endif %}
        </div>

In the future that may be added automatically and manageable by the modules.

1 Like