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.
option
-variable inside your for-loop to see if it contains a reference to a country-code? – Fuglsetrampen Mar 2 '18 at 12:44{{ 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