Answers.Body: <p>This would work for both with or without <code>optgroup</code> (replace <code>aDropdown</code> with your fieldname):</p>
  
  <pre class="lang-twig prettyprint-override"><code>{% set field = craft.app.fields.getFieldByHandle('aDropdown') %}
  
  {# If we have optgroups in this field, set a counter to 0 else return false #}
  
  {% set optgroupCount = field.optgroups ? 0 : false %}
  &lt;select name="fields[aDropdown]"&gt;
      {% for option in field.options %}
  
          {% if option.optgroup is not defined %}
              {# This is a regular option #}
              {% set selected = entry is defined ? entry.aDropdown.value == option.value : option.default %}
              &lt;option value="{{ option.value }}" {% if selected %}selected{% endif %}&gt;
                  {{ option.label }}
              &lt;/option&gt;
          {% else %}
              {# This is an optgroup #}
              {# Check if we should close a group before opening a new one.#}
              {{ optgroupCount &gt; 0 ? '&lt;/optgroup&gt;' }}
              &lt;optgroup label="{{ option.optgroup }}"&gt;
              {% set optgroupCount = optgroupCount + 1 %}
          {% endif %}
  
      {% endfor %}
      {# If we had optgroups better close the last one #}
      {{ optgroupCount ? '&lt;/optgroup&gt;' }}
  &lt;/select&gt;
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2019-09-01T16:19:01.297Z
Answers.LastActivityDate: 2019-09-01T16:19:01.297Z
Answers.UserDisplayName: Oli
Answers.Body: <p>FYI, small change to get this working in Craft 4 via Craft support/Oli. Change the 3rd line in the above to:</p>
  <pre><code>{% set optgroupCount = 'optgroup' in 'field.options.optgroup' ? 0 : false %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2023-11-21T14:22:39.627Z
Answers.LastActivityDate: 2023-11-21T14:22:39.627Z
Answers.UserDisplayName: Ian Ebden
Body: <p>The display of a <a href="https://docs.craftcms.com/v3/dropdown-fields.html#templating" rel="nofollow noreferrer">dropdown</a> without optgroups is defined:</p>
  
  <pre><code>{% set field = craft.app.fields.getFieldByHandle('&lt;FieldHandle&gt;') %}
  
  &lt;select name="fields[&lt;FieldHandle&gt;]"&gt;
      {% for option in field.options %}
  
          {% set selected = entry is defined
              ? entry.&lt;FieldHandle&gt;.value == option.value
              : option.default %}
  
          &lt;option value="{{ option.value }}"
                  {% if selected %}selected{% endif %}&gt;
              {{ option.label }}
          &lt;/option&gt;
      {% endfor %}
  &lt;/select&gt;
  </code></pre>
  
  <p>The following error occurs:</p>
  
  <pre><code>Key "default" for array with keys "optgroup" does not exist.
  </code></pre>
  
Tags: <craft3><plugin-craftcommerce><plugin-craftcommerce2>
Title: How-to display a dropdown field with optgroups?
ThreadText: # How-to display a dropdown field with optgroups?
  
  (Asked by: username_0 on 2019-08-31)
  
  The display of a [dropdown](https://docs.craftcms.com/v3/dropdown-fields.html#templating) without optgroups is defined:
  
  ```
  {% set field = craft.app.fields.getFieldByHandle('<FieldHandle>') %}
  
  <select name="fields[<FieldHandle>]">
      {% for option in field.options %}
  
          {% set selected = entry is defined
              ? entry.<FieldHandle>.value == option.value
              : option.default %}
  
          <option value="{{ option.value }}"
                  {% if selected %}selected{% endif %}>
              {{ option.label }}
          </option>
      {% endfor %}
  </select>
  
  ```
  
  The following error occurs:
  
  ```
  Key "default" for array with keys "optgroup" does not exist.
  
  ```
  
  **Tags:** craft3, plugin-craftcommerce, plugin-craftcommerce2
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2019-09-01. Score: 3 | Accepted answer
  
  This would work for both with or without `optgroup` (replace `aDropdown` with your fieldname):
  
  ```twig
  {% set field = craft.app.fields.getFieldByHandle('aDropdown') %}
  
  {# If we have optgroups in this field, set a counter to 0 else return false #}
  
  {% set optgroupCount = field.optgroups ? 0 : false %}
  <select name="fields[aDropdown]">
      {% for option in field.options %}
  
          {% if option.optgroup is not defined %}
              {# This is a regular option #}
              {% set selected = entry is defined ? entry.aDropdown.value == option.value : option.default %}
              <option value="{{ option.value }}" {% if selected %}selected{% endif %}>
                  {{ option.label }}
              </option>
          {% else %}
              {# This is an optgroup #}
              {# Check if we should close a group before opening a new one.#}
              {{ optgroupCount > 0 ? '</optgroup>' }}
              <optgroup label="{{ option.optgroup }}">
              {% set optgroupCount = optgroupCount + 1 %}
          {% endif %}
  
      {% endfor %}
      {# If we had optgroups better close the last one #}
      {{ optgroupCount ? '</optgroup>' }}
  </select>
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2023-11-21. Score: 1
  
  FYI, small change to get this working in Craft 4 via Craft support/Oli. Change the 3rd line in the above to:
  
  ```
  {% set optgroupCount = 'optgroup' in 'field.options.optgroup' ? 0 : false %}
  
  ```
  
  
  ---
  
  