Referencing a specific product variant (liquid)

Solved
Danh11
Tourist
9 0 1

I'm trying to retrieve the variant colors of products in liquid.

I'm looking at the JSON object and I can see the key for the variants are named 'option1', 'option2' and so on, but this isn't specifically targeting the color. The client isn't going to remember to add variants in a particular order so that option1 and option2 line up to the right values.

Is there a way around this to achieve what I'm after?

Accepted Solution (1)

Accepted Solutions
Evgeniy-M
Excursionist
35 6 7

This is an accepted solution.

Hey @Danh11 .

You can ask the client to give the "Color" name to the option and then you will be available to retrieve all the product's colors using the product.options_by_name:

 

<label>
  Color
  <select>
    {% for color_option in product.options_by_name['Color'].values %}
      <option>{{ color_option }}</option>
    {% endfor %}
  </select>
</label>

 

Liquid Ajax Cart — a Javascript library to build Shopify Ajax Carts using plain Liquid templates.

View solution in original post

Replies 2 (2)
Evgeniy-M
Excursionist
35 6 7

This is an accepted solution.

Hey @Danh11 .

You can ask the client to give the "Color" name to the option and then you will be available to retrieve all the product's colors using the product.options_by_name:

 

<label>
  Color
  <select>
    {% for color_option in product.options_by_name['Color'].values %}
      <option>{{ color_option }}</option>
    {% endfor %}
  </select>
</label>

 

Liquid Ajax Cart — a Javascript library to build Shopify Ajax Carts using plain Liquid templates.

View solution in original post

Danh11
Tourist
9 0 1

Ah thanks! I missed that in the docs.