Updating options values of Product via Admin API

Vlad_BWT
Shopify Partner
2 0 1

Hello!

I've created product, its options and its variants via endpoint POST /admin/api/2022-10/products.json:

 

{
  "product": {
    "title": "Main Product",
    "body_html": null,
    "vendor": "Brand name",
    "options": [
      {
        "name": "Color",
        "values": [
          "Red",
          "White"
        ]
      },
      {
        "name": "Size",
        "values": [
          "S",
          "M",
          "L"
        ]
      }
    ],
    "variants": [
      {
        "title": "Prod 1",
        "sku": null,
        "barcode": null,
        "option1": "White",
        "option2": "S"
      },
      {
        "title": "Prod 2",
        "sku": null,
        "barcode": null,
        "option1": "Red",
        "option2": "M"
      },
      {
        "title": "Prod 3",
        "sku": null,
        "barcode": null,
        "option1": "Red",
        "option2": "L"
      }
    ]
  }
}

 

Then, I've tryed to update this product to add option value "Green" via endpoint PUT /admin/api/2022-10/products/12345.json:

 

{
    "product": {
        "id": 12345,
        "title": "Main product",
        "body_html": null,
        "vendor": "Brand name",
        "options": [
            {
                "id": 111,
                "name": "Color",
                "position": 1,
                "values": [
                    "White",
                    "Red",
                    "Green"
                ]
            },
            {
                "id": 222,
                "name": "Size",
                "position": 2,
                "values": [
                    "S",
                    "M",
                    "L"
                ]
            }
        ]
    }
}

 

If I try to change option name, this endpoint work perfectly. But options values didn't change.

Also I looked for a way to update options values via GraphQL Admin API. But a working solution couldn't be reached.

Can anyone suggest a way to update option values via Admin API (REST or GraphQL doesn't matter)?

Thanks in advance!

Reply 1 (1)

ShopifyDevSup
Shopify Staff
1249 202 443

Hi @Vlad_BWT!

You generally don't want to use the product endpoint to make changes to product variants (option) as it tends to be destructive. For example, when updating a product's variants, if you don't include the IDs of every other variant that you're not updating those variants will be deleted! So, if you find yourself using the product endpoint to make changes to variants and something "isn't working", it's probably to save you the headache of accidentally destroying your work.

That's some more general advice but to answer your question you would want to use either the REST Product Variant resource or the GraphQL productVariantUpdate mutation. The REST page includes an example of changing the name of an option (the PUT example).

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