<p>Not particularly proud of this code, but if you want to do it from Twig, something like this:</p>

<pre><code>{% set countryList = ['United Kingdom', 'United States'] %}

{% for country in craft.commerce.countriesList() %}
    {% if country != 'United Kingdom' and country != 'United States' %}
        {% set countryList = countryList|merge([country]) %}
    {% endif %}
{% endfor %}

&lt;ul&gt;
    {% for country in countryList %}
        &lt;li&gt;{{ country }}&lt;/li&gt;
    {% endfor %}
&lt;/ul&gt;
</code></pre>

CC BY-SA 3.0
2016-03-23T18:34:48.783Z
2016-03-23T18:34:48.783Z
Brad Bell
<p>I needed to add the country values to the array so used <a href="https://craftcms.stackexchange.com/a/14293/4466">Brad's answer</a> to come up with the following:</p>

<pre><code>{% set countryList = [{'77':'United Kingdom'},{'233':'United States'}] %}
{% for key, country in craft.commerce.countriesList %}
  {% if country != 'United Kingdom' and country != 'United States' %}
    {% set key_string = key|json_encode() %}
    {% set countryList = countryList|merge([{(key_string):country}]) %}
  {% endif %}
{% endfor %}
</code></pre>

<p>The above builds the array of countries with their values. The below is the code to loop over the array and fill a <code>select</code> menu with their values.</p>

<pre><code>&lt;select&gt;
  {% for country in countryList %}
    {% for key, option in country %}
      {% set optionValue = (model ? model.countryId : '') %}
      &lt;option value="{{ key }}" {% if key == optionValue %} selected{% endif %}&gt;{{ option }}&lt;/option&gt;
    {% endfor %}
  {% endfor %}
&lt;/select&gt;
</code></pre>

CC BY-SA 3.0
2016-08-26T15:02:26.453Z
2016-08-26T15:02:26.453Z
2017-04-13T12:51:00.303Z
Luke Pearce
<p>Is there a way I can have the array from <code>craft.commerce.countriesList</code> automatically default to the country my store is in?</p>

CC BY-SA 3.0
2016-03-23T17:25:45.177Z
What do you mean automatically default?  `craft.commerce.countriesList` just returns an array of `id` => `name` values from the `craft_commerce_countries` table in the database. You can do whatever you want with it.
Brad Bell
CC BY-SA 3.0
2016-03-23T17:57:46.887Z
well, if I'm looping over them in a select list, I'd want the UK (and perhaps the US) to be at the top of that list, rather than Afghanistan.
Steven Grant
CC BY-SA 3.0
2016-03-23T18:16:49.557Z
I'm playing fake Luke Holder here, but I don't believe Commerce has a specific setting to specify what country your store is in.  Maybe you want to match it by currency (messy) or by the locale Craft is currently using (slightly less messy).
Brad Bell
CC BY-SA 3.0
2016-03-23T18:22:00.037Z
You're entirely correct, I was confusing matters with default currency. I guess I'm looking for a way with Twig to have UK appear first in the loop.
Steven Grant
<templating><plugin-craftcommerce><ecommerce>
Have the country in craft.commerce.countriesList default
# Have the country in craft.commerce.countriesList default

(Asked by: username_0 on 2016-03-23)

Is there a way I can have the array from `craft.commerce.countriesList` automatically default to the country my store is in?

**Tags:** templating, plugin-craftcommerce, ecommerce

> **Comments:**
> **username_1**: What do you mean automatically default?  `craft.commerce.countriesList` just returns an array of `id` => `name` values from the `craft_commerce_countries` table in the database. You can do whatever you want with it.  
> **username_0**: well, if I'm looping over them in a select list, I'd want the UK (and perhaps the US) to be at the top of that list, rather than Afghanistan.  
> **username_1**: I'm playing fake Luke Holder here, but I don't believe Commerce has a specific setting to specify what country your store is in.  Maybe you want to match it by currency (messy) or by the locale Craft is currently using (slightly less messy).  
> **username_0**: You're entirely correct, I was confusing matters with default currency. I guess I'm looking for a way with Twig to have UK appear first in the loop.  

---

## 2 answers

### Answer by username_1 on 2016-03-23. Score: 3

Not particularly proud of this code, but if you want to do it from Twig, something like this:
{% set countryList = ['United Kingdom', 'United States'] %}

{% for country in craft.commerce.countriesList() %}
    {% if country != 'United Kingdom' and country != 'United States' %}
        {% set countryList = countryList|merge([country]) %}
    {% endif %}
{% endfor %}


    {% for country in countryList %}
        {{ country }}
    {% endfor %}


---

### Answer by username_2 on 2016-08-26. Score: 3

I needed to add the country values to the array so used Brad's answer to come up with the following:
{% set countryList = [{'77':'United Kingdom'},{'233':'United States'}] %}
{% for key, country in craft.commerce.countriesList %}
  {% if country != 'United Kingdom' and country != 'United States' %}
    {% set key_string = key|json_encode() %}
    {% set countryList = countryList|merge([{(key_string):country}]) %}
  {% endif %}
{% endfor %}

The above builds the array of countries with their values. The below is the code to loop over the array and fill a select menu with their values.

  {% for country in countryList %}
    {% for key, option in country %}
      {% set optionValue = (model ? model.countryId : '') %}
      {{ option }}
    {% endfor %}
  {% endfor %}


---

