Answers.Body: <p>You would currently need to loop over all variants and just grab the ones that match the 'if' condition shown below:</p>
  
  <pre><code>{% set colourSlug = craft.request.getSegment(4) %}
  
  {% if not colourSlug %}
      {# Grab first product variant #}
      {% set dressColour = product.defaultVariant %}
  {% else %}
      {# Set dress colour #}
      {% for variant in product.variants %}
      {% if variant.colour == colorSlug %}
         {{variant.sku}}
      {% endif %}
      {% endfor %}
  {% endif %}
  </code></pre>
  
  <p>We will look to enable variants to be an element criteria query. If each variant has their own page, would it make sense they are seperate products?</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-04T17:54:19.803Z
Answers.LastActivityDate: 2015-12-04T17:54:19.803Z
Answers.UserDisplayName: Luke Holder
Answers.Body: <p>Just before I saw luke's answer I wrote this:</p>
  
  <pre><code>{# Grab colour segment #}
  {% set colourSlug = craft.request.getSegment(4) %}
  
  {# If there is no colour, we grab the first variant colour #}
      {% if not colourSlug %}
          {# Grab first product variant #}
          {% set dressVariantActive = product.defaultVariant %}
      {% else %}
          {# Set outside the loop #}
          {% set dressVariantActive = '' %}
  
          {% for dressVariantSelected in product.variants %}
              {% if dressVariantSelected.colour.value == colourSlug %}
                  {% set dressVariantActive = dressVariantSelected %}
              {% endif %}
          {% endfor %}
          {% if not dressVariantActive %}{% exit 404 %}{% endif %}
      {% endif %}
  </code></pre>
  
  <p>I wanted to include this as I found out a useful thing: Defining a variable inside a <code>for</code> loop is <strong>only</strong> possible if you set the variable before the loop.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-04T18:09:59.597Z
Answers.LastActivityDate: 2015-12-04T18:09:59.597Z
Answers.UserDisplayName: Rob Hodges
Body: <p>Excuse my new-ness, but I'm having an issue with product variants. </p>
  
  <p><strong>Scenario</strong></p>
  
  <p>I have a product with multiple variants (up to 30 in fact). Based on the URL, I want to choose a specific variant so that I can display fields from that variant on my product detail page. I also want a 'default variant' if there is no variant specified in the URL. </p>
  
  <p>In my example, the variant should be selected by a custom field in that variant <code>colour</code> whose value match the 4th URL segment.</p>
  
  <p><strong>Code</strong></p>
  
  <pre><code>{# Grab colour segment #}
  {% set colourSlug = craft.request.getSegment(4) %}
  
  {# If there is no colour, we grab the first variant colour #}
      {% if not colourSlug %}
          {# Grab first product variant #}
          {% set dressColour = product.defaultVariant %}
      {% else %}
          {# Set dress colour #}
          ??{% set dressColour = product.variants.find(colour.colourSlug) %}??
      {% endif %}
  </code></pre>
  
Tags: <templating><plugin-craftcommerce>
Title: How do I select a specific product variant by element/field/entry?
ThreadText: # How do I select a specific product variant by element/field/entry?
  
  (Asked by: username_0 on 2015-12-04)
  
  Excuse my new-ness, but I'm having an issue with product variants.
  
  **Scenario**
  
  I have a product with multiple variants (up to 30 in fact). Based on the URL, I want to choose a specific variant so that I can display fields from that variant on my product detail page. I also want a 'default variant' if there is no variant specified in the URL.
  
  In my example, the variant should be selected by a custom field in that variant `colour` whose value match the 4th URL segment.
  
  **Code**
  
  ```
  {# Grab colour segment #}
  {% set colourSlug = craft.request.getSegment(4) %}
  
  {# If there is no colour, we grab the first variant colour #}
      {% if not colourSlug %}
          {# Grab first product variant #}
          {% set dressColour = product.defaultVariant %}
      {% else %}
          {# Set dress colour #}
          ??{% set dressColour = product.variants.find(colour.colourSlug) %}??
      {% endif %}
  
  ```
  
  **Tags:** templating, plugin-craftcommerce
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2015-12-04. Score: 2 | Accepted answer
  
  You would currently need to loop over all variants and just grab the ones that match the 'if' condition shown below:
  
  ```
  {% set colourSlug = craft.request.getSegment(4) %}
  
  {% if not colourSlug %}
      {# Grab first product variant #}
      {% set dressColour = product.defaultVariant %}
  {% else %}
      {# Set dress colour #}
      {% for variant in product.variants %}
      {% if variant.colour == colorSlug %}
         {{variant.sku}}
      {% endif %}
      {% endfor %}
  {% endif %}
  
  ```
  
  We will look to enable variants to be an element criteria query. If each variant has their own page, would it make sense they are seperate products?
  
  
  ---
  
  ### Answer by username_0 on 2015-12-04. Score: 1
  
  Just before I saw username_1's answer I wrote this:
  
  ```
  {# Grab colour segment #}
  {% set colourSlug = craft.request.getSegment(4) %}
  
  {# If there is no colour, we grab the first variant colour #}
      {% if not colourSlug %}
          {# Grab first product variant #}
          {% set dressVariantActive = product.defaultVariant %}
      {% else %}
          {# Set outside the loop #}
          {% set dressVariantActive = '' %}
  
          {% for dressVariantSelected in product.variants %}
              {% if dressVariantSelected.colour.value == colourSlug %}
                  {% set dressVariantActive = dressVariantSelected %}
              {% endif %}
          {% endfor %}
          {% if not dressVariantActive %}{% exit 404 %}{% endif %}
      {% endif %}
  
  ```
  
  I wanted to include this as I found out a useful thing: Defining a variable inside a `for` loop is **only** possible if you set the variable before the loop.
  
  
  ---
  
  