2

Here's some code from Craft Commerce's examples:

<form method="POST" accept-charset="UTF-8" class="ui form validate">
    {{ getCsrfInput() }}
    <input type="hidden" name="action" value="commerce/cart/updateCart" />

    [...]

    {% set countries = craft.commerce.countries %}
    {% if countries|length > 1 %}
        <div class="field{% if model and model.getErrors('countryId') %} error{% endif %}">
            <label>{{ "Country"|t }}</label>
            <select name="{{ modelName }}[countryId]" class="ui fluid dropdown">
                <option value="">{{ "Choose Country"|t }}</option>
                {% for country in countries %}
                    {% set optionValue = (model ? model.countryId : '') %}
                    <option value="{{ country.id }}"{% if country.id == optionValue %} selected="selected"{% endif %}>{{ country.name }}</option>
                    <div class="item" data-value="">{{ country.name }}</div>
                {% endfor %}
            </select>
            {% if model and model.getErrors('countryId') %}
                <span class="error">{{ model.getErrors('countryId')|join }}</span>
            {% endif %}
        </div>
    {% endif %}

    [...]

</form>

I wonder if it's somehow possible to work with the country code (e.g. US, CH...) instead of the country id of Craft Commerce (1..256). So basically, countryId vs. countryIso?

I can't find any information in the docs.

It might be a small detail, but when using form autofill with programs like 1password, it won't be able to fill in the country automatically when using an id vs. a country code.

  • Have you tried dumping the option-variable inside your for-loop to see if it contains a reference to a country-code? – Fuglsetrampen Mar 2 '18 at 12:44
  • Hi Fuglsetrampen, could you maybe elaborate on how to dump the option variable? – outline4 Mar 3 '18 at 14:00
  • Dumping something is as easy as {{ dump(option) }} (inside the for-loop in your case). Now, what I would probably do in your situation (based on the updated / rewritten question) is: Give my select a "bogus" name, print the ISO-code as the value of the options, print the ID as a data-attribute of the options, have a hidden [countryId]-field, use JS to update the value of my hidden field based on the data-attribute of the selected option in a event-handler. – Fuglsetrampen Mar 5 '18 at 7:58
1

This should do it:

{% set countries = craft.commerce.countries %}

<ul>
{% for country in countries %}
    <li>{{ country.iso }}</li>
{% endfor %}
</ul>
|improve this answer|||||
  • Thanks! But will this actually work with the tax rates and shipping methods? Asking for a friend 😜 – outline4 Mar 2 '18 at 15:51
  • I've updated the question above. it uses ´craft.commerce.countriesList´. how, in this case, would the option values and error messages look like? – outline4 Mar 2 '18 at 16:10
  • Why use craft.commerce.countriesList? It's going to be a lot more inefficient than just going through craft.commerce.countries directly. – Brad Bell Mar 3 '18 at 0:26
  • Hi, I am not trying to output only a countries list. I was asking if it is possible to use the ISO value vs. the ID of a country within the checkout process. Turns out, that you'll have to use the country ID in checkout? It just won't accept the country code, or am I wrong? – outline4 Mar 3 '18 at 9:04
  • I've updated the answer to better reflect where we are... – outline4 Mar 3 '18 at 9:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.