How do I determine the name of the variant option in the Shopify product export?

The Shopify API only provides the values of the variant options in the fields option1, option2, and option3 where the ordering of the options could vary per product (e.g. option1 might be the color option for one but the size for another). It's also possible that certain products in your store have completely different sets of options that can be assigned to their variants.

In EZ Exporter, we can figure out the name of the option by searching the list of all available options for what the position should be for each one using the Calculated Fields feature.

For example, you may be using the following options for a product:

  • Color
  • Size
  • Material

The Shopify API's options field will store the data like this:

[
   {
      "name":"Size",
      "position":1,
      "values":[
         "Small",
         "Medium"
      ]
   },
   {
      "name":"Color",
      "position":2,
      "values":[
         "Red",
         "Blue"
      ]
   },
   {
      "name":"Material",
      "position":3,
      "values":[
         "Leather",
         "Cotton"
      ]
   }
]

The key to figuring out which variant option field is which is by matching up the position value with the number in the variants.optionX field. For example, a variants.option1 maps to position 1 in the product options.

To do this, we can use a Calculated Field formula like this:

{{ variants.option1 }} if search_attributes({{ options }}, "Color", result_key="position") == 1 else {{ variants.option2 }} if search_attributes({{ options }}, "Color", result_key="position") == 2 else {{ variants.option3 }} if search_attributes({{ options }}, "Color", result_key="position") == 3 else ""

The above will search for the Color option in the data, check if the the number in the different variants.optionX field matches the position, and then pull that value if there's a match.

We can then repeat the formula for the other options and just replacing the option name "Color" with the other option names.

Retrieve the Size option:

{{ variants.option1 }} if search_attributes({{ options }}, "Size", result_key="position") == 1 else {{ variants.option2 }} if search_attributes({{ options }}, "Size", result_key="position") == 2 else {{ variants.option3 }} if search_attributes({{ options }}, "Size", result_key="position") == 3 else ""

Retrieve the Material option:

{{ variants.option1 }} if search_attributes({{ options }}, "Material", result_key="position") == 1 else {{ variants.option2 }} if search_attributes({{ options }}, "Material", result_key="position") == 2 else {{ variants.option3 }} if search_attributes({{ options }}, "Material", result_key="position") == 3 else ""

The output will look something like this:

EZ Exporter - Export Shopify Product Variant OPtions


App: EZ Exporter

Tags: advanced features, calculated fields, variants