New Shopify Certification now available: Liquid Storefronts for Theme Developers

Cannot reorder product option values via REST API

masonmcelvain
Shopify Partner
6 1 1

Problem

The REST API (2023-01) does not seem to support reordering option values on the product resource.

 

Example

When creating or updating a product, clients can send an `options` field with a list of associated values.

 

{
   ...,
   "options": [
      {
        "name": "Apparel Size",
        "values": [
          "Men's Small",
          "Men's Medium",
          "Men's Large"
          "Men's XL",
          "Men's XXL",
          "Women's Small",
          "Women's Medium",
          "Women's Large",
          "Women's XL",
          "Women's XXL",
        ]
      },
   ],
   ...,
}

 

 

Expected Behavior

The sort order of the option values is preserved when the product is created or updated.

 

Actual Behavior

The option values appear in a different sort order on the product. For example, when viewing myshopify.com/admin/products/<id>.json or getting the product via the REST API, the sort order of the option values appears like this for my case:

 

{
   ...,
   "options": [
      {
        "id": 594680422,
        "product_id": 632910392,
        "name": "Apparel Size",
        "position": 1,
        "values": [
          "Men's XL",
          "Men's XXL",
          "Women's Small",
          "Women's Medium",
          "Women's Large",
          "Women's XL",
          "Women's XXL",
          "Men's Small",
          "Men's Medium",
          "Men's Large"
        ]
      },
   ],
   ...,
}

 

 

Relevance

Merchants rely on the order of option values to display frontend components that list the options in a meaningful order. A dropdown that contains:

S, M, L, XL, XXL

is much easier to use than one that contains:

XL, M, S, XXL, L

 

Related

I think this is a duplicate of this post by @dansul , but that post references the 2022-07 API version.

Replies 5 (5)
Shayne
Shopify Staff
Shopify Staff
253 19 63

If the product was created with it's options in a different order than you want the variants to display in, you need to reorder the variants themselves on the product.

The option values that exist in the `options` array on the product will orient themselves to match the position of the variants on the product.

Shayne | Developer Advocate @ Shopify 
 - Was my reply helpful? Click Like to let me know! 
 - Was your question answered? Mark it as an Accepted Solution
 - To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

masonmcelvain
Shopify Partner
6 1 1

Hi Shayne, thanks for your replay and explaining how the API works.

 

While reordering the variants is a simple solution if you have only one option, it fails when there are multiple options.

 

Say that in addition to "Size", I also want to have a "Color" option with the values "Blue" and "Pink". I want Pink to come before Blue, and I want the "Size" listed from smallest to largest.

 

My business sells Blue shirts in Small and Medium, and Pink shirts in Medium and Large.

 

Observe that there is no possible order of variants that meet my business's needs. No matter how I order them, the "Size" or "Color" values will not be in the desired order.

 

This request to the API:

 

{
   "options": [
      {
         "name": "Size",
         "values": [
           "Small",
           "Medium",
           "Large"
         ]
      },
      {
         "name": "Color",
         "values": [
           "Pink",
           "Blue"
         ]
      }
   ],
   "variants": [
      {
         ...,
         "option1": "Small",
         "option2": "Blue"
      },
      {
         ...,
         "option1": "Medium",
         "option2": "Blue"
      },
      {
         ...,
         "option1": "Medium",
         "option2": "Pink"
      },
      {
         ...,
         "option1": "Large",
         "option2": "Pink"
      }
   ],
   ...,
}

 

will produce the following result:

 

{
   ...,
   "options": [
      {
        "name": "Size",
        "position": 1,
        "values": [
           "Small",
           "Medium",
           "Large"
        ]
      },
      {
        "name": "Color",
        "position": 2,
        "values": [
           "Blue",
           "Pink"
        ]
      },
   ],
   ...,
}

 

 

And making the request with pink variants before blue ones would result in the "Size" option values being out of order.

 

Whether you call this a bug report or a feature request, the API should allow clients to sort option values independently of the variant sort order.

ShopifyDevSup
Shopify Staff
Shopify Staff
1203 190 420

Hey folks - just jumping in here to say I'll forward this along to our product team. I can't guarantee anything, but I do appreciate your run down on this @masonmcelvain - really good use case - it makes sense that this should be considered/looked into. 

Thanks again for reaching out!

 

Al | Shopify Developer Support 

Developer Support @ Shopify
- Was this reply helpful? Click Like to let us know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog

RossellaF
Shopify Partner
21 0 5

Hi, 

 

I am encountering the same use case where I have products with size and color options. I was wondering if there was any update in this. 

 

Thanks! 

alonram
Shopify Partner
1 0 0

Hi,

 

I also need an option to sort Option values. The other way is to sort all variants every time a single variant is added to the product. In some products, I reach the limit of 100 variants and require a lot of API calls. My system adds variants dynamically.

 

Thanks.