CartThrob 9 documentation for ExpressionEngine commerce teams Visit CartThrob.com
CartThrob.com

Country Select

country_select outputs an HTML <select> of countries from CartThrob locales data.

To edit country lists, see Locales Config.

See also:

Parameters

name

Name attribute of the generated <select>.

Default: country.

name="country_code"

selected

Currently selected country value.

selected="USA"

default

Fallback selected value when selected is not provided.

default="USA"

orderby

Sort order mode for options.

Default: name (sort by country name). Any non-name value sorts by option key/value code.

orderby="name"

orderby="code"

alpha2

When country codes are enabled, use alpha-2 codes instead of alpha-3 codes for option values.

alpha2="yes"

country_codes

Controls whether option values are country codes or country names.

Default: yes (code values). no uses country names as option values.

country_codes="yes"

country_codes="no"

id

id attribute for the <select>.

id="country_code"

class

class attribute for the <select>.

class="country-select"

onchange

Inline JavaScript onchange attribute.

onchange="this.form.submit()"

extra

Additional raw HTML attributes appended to the <select>.

extra='data-role="country-select"'

add_blank

Adds a blank --- option at the top.

add_blank="yes"

Output

Returns an HTML <select> element.

Option values depend on country_codes and alpha2:

  1. country_codes="yes" + alpha2="no" (default): values are alpha-3 codes (for example USA).
  2. country_codes="yes" + alpha2="yes": values are alpha-2 codes (for example US).
  3. country_codes="no": values are country names.

Behavior and Constraints

  1. Country list comes from ee()->locales->countries(alpha2, country_codes).
  2. Locale filtering (cartthrob:locales_countries) can limit which countries are included.
  3. selected takes precedence; default is used only as fallback.
  4. Sorting defaults to name (orderby="name"); otherwise keys/codes are sorted.
  5. id, class, onchange, and extra are passed through to the generated <select>.

Examples

Basic usage:

{exp:cartthrob:country_select name="country_code" selected="{customer_country_code}"}

Save/update country in a form:

{exp:cartthrob:save_customer_info_form return="cart/view_cart"}
    {exp:cartthrob:country_select name="country_code" selected="{customer_country_code}"}
    <input type="submit" value="Update Your Country" />
{/exp:cartthrob:save_customer_info_form}

Use alpha-2 code values and a blank option:

{exp:cartthrob:country_select
    name="shipping_country_code"
    alpha2="yes"
    add_blank="yes"
    selected="{customer_shipping_country_code}"
}