What's your biggest current challenge? Have your say in Community Polls along the right column.
Our Partner & Developer boards on the community are moving to a brand new home: the .dev community forums! While you can still access past discussions here, for all your future app and storefront building questions, head over to the new forums.

Relationships between Products, Options and Variations (Admin API)

Solved

Relationships between Products, Options and Variations (Admin API)

witchbag
Visitor
2 0 0

Can anyone help me to understand the relationships between Products, Options and Variations?

I would like to insert a "top level" product (e.g. Jeans) which will be available in three sizes (e.g. Small, Medium, Large)
and three colours (e.g. Black, Blue, Green). At this point no stock has been ordered so I want to leave it at that, i.e. no actual sellable variations yet.

I thus thought to insert the "top level" product with a POST to https://{my_shop}.myshopify.com/admin/api/2021-01/products.json as:-

{
  "product": {
    "title": "Jeans",
    "options": [
      {
        "name": "Size",
        "values": [
          "Small",
          "Medium",
          "Large"
        ]
      },
      {
        "name": "Colour",
        "values": [
          "Black",
          "Blue",
          "Green"
        ]
      }
    ]
  }
}

which results in the response:-

{
    "errors": {
        "base": [
            "You need to add option values for Colour"
        ]
    }
}

My intention was to use this "top level" product and "attach" variants when the merchant obtains stock ( which may well not be all combinations of the above options - Green may only be available in Medium).

I tried swapping the order of the options but that simply changed the error to "You need to add option values for Size". I then tried removing the "Colour" option with:-

{
  "product": {
    "title": "Jeans",
    "options": [
      {
        "name": "Size",
        "values": [
          "Small",
          "Medium",
          "Large"
        ]
      }
    ]
  }
}

the Response was:-

{
    "product": {
        "id": 6211926163652,
        "title": "Jeans",
        "body_html": null,
        "vendor": "witchbag",
        "product_type": "",
        "created_at": "2021-02-22T13:29:58+00:00",
        "handle": "jeans",
        "updated_at": "2021-02-22T13:29:58+00:00",
        "published_at": "2021-02-22T13:29:58+00:00",
        "template_suffix": null,
        "status": "active",
        "published_scope": "web",
        "tags": "",
        "admin_graphql_api_id": "gid://shopify/Product/6211926163652",
        "variants": [
            {
                "id": 38129887576260,
                "product_id": 6211926163652,
                "title": "Default Title",
                "price": "0.00",
                "sku": "",
                "position": 1,
                "inventory_policy": "deny",
                "compare_at_price": null,
                "fulfillment_service": "manual",
                "inventory_management": null,
                "option1": "Default Title",
                "option2": null,
                "option3": null,
                "created_at": "2021-02-22T13:29:58+00:00",
                "updated_at": "2021-02-22T13:29:58+00:00",
                "taxable": true,
                "barcode": null,
                "grams": 0,
                "image_id": null,
                "weight": 0.0,
                "weight_unit": "kg",
                "inventory_item_id": 40223382241476,
                "inventory_quantity": 0,
                "old_inventory_quantity": 0,
                "requires_shipping": true,
                "admin_graphql_api_id": "gid://shopify/ProductVariant/38129887576260"
            }
        ],
        "options": [
            {
                "id": 7912966488260,
                "product_id": 6211926163652,
                "name": "Size",
                "position": 1,
                "values": [
                    "Default Title"
                ]
            }
        ],
        "images": [],
        "image": null
    }
}

 

This created a product with a single Variation (according to the Response) and on my Catalog I have a Product called "Jeans" with a single drop down list labelled "Size" containing a single item "Default Title".

You may wonder why I don't just wait until there are existing Variations of actual product; well this is the way the Fashion industry often works, sometimes with pre-ordered "drop offs". They may also want to create a page with a "coming soon" type of theme.

Any help or explanation(s) or clarification(s) would be much appreciated.

 

Accepted Solution (1)
syf_
Shopify Staff
95 21 25

This is an accepted solution.

Hi Derek,

Yes, you're right. Even if you don't create a variant for the product, Shopify creates one under the hood.

To answer your questions,

  1. There is an options array that specifies the different options for variants but you cannot specify an option value and not have any variant utilise that option value; any such values without an associated variant would be ignored. Therefore, the values specified for an option are actually ignored, because it is populated using the variant options. If you take a look at the section to add a variant on the Admin store, the values for the options are not presented as a drop down, however, a drop down is available on the customer-facing storefront. In essence, if you want a value to be part of the options array, then a variant has to exist that specifies a value for that option. If the variant isn't available, then you can set its quantity to zero.
  2. To create additional variants, you would need the Product Variant resource  to do this.

If you had any additional questions, let me know.

Best,
Seth.

syf_ | Developer Support @ 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

View solution in original post

Replies 3 (3)

syf_
Shopify Staff
95 21 25

Hi @witchbag,

I'll try to throw some light on this for you.

TL;DR Every product has a variant and every variant specifies the value for an option. When you create a product without variants, Shopify still creates a variant associated with the product and specifies an option for that variant. Shopify specifies the Title option in this case with a value of "Default Title". In essence you can't have a product without a variant and a variant that doesn't have at least one option (or an option if a variant doesn't specify a value for that option). The reason your response shows "Default Title" for a Size option is because you didn't create a variant so Shopify created one for you but because you included the options object in your request body and specified Size, that was used instead of the Title option.

To actually create a variant, you need to specify the variant object to be created. The order in which you specify options are important as that is what you'll use to indicate the options (option1, option2, ...) for the variants. For all options you specify in the options object, you need to specify a value for them in the variants object. See sample code below that creates a "Jeans" product having a variant with Colour option "Green" and Size option "Medium".

 

{
  "product": {
    "title": "Jeans",
    "variants": [
        {
            "option1": "Medium",
            "option2": "Green"
        }
    ],
    "options": [
      {
        "name": "Size"
      },
      {
        "name": "Colour"
      }
    ]
  }
}

 

Hopefully this helps clarify things for you. If you have any additional questions, let me know.

Best,
Seth.

syf_ | Developer Support @ 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

witchbag
Visitor
2 0 0

Hello Seth,

thanks for your help. So it seems that I can't have a Product with no actual variants; I'll have to re-think my strategy here - that's OK, I have an idea how I can get around that.

It brings me to a couple of further questions:-

1. From somewhere, I got the idea that the options object could have a values member array with what I thought would be the contents of the drop down for that option "type" e.g.

 "options": [
   {
     "name": "Size",
     "values": [
       "Small",
       "Medium",
       "Large"
     ]
   },
   {
     "name": "Colour",
     "values": [
       "Blue",
       "Orange",
       "Red",
       "Sky Blue Pink"
     ]
   }
 ],

 

Did I just dream that? If not where does the values array fit in to the picture?

 

2. Let's suppose that I create my initial Products "Jeans" with a few Variations "Green" "Small", "Black" "Small" and "Blue" "Small" and I later want to add some more Variations "Black" "Medium" and "Blue" "Medium" but I want to leave the original Variations undisturbed. How do I do that?

best regards,

Derek

 

syf_
Shopify Staff
95 21 25

This is an accepted solution.

Hi Derek,

Yes, you're right. Even if you don't create a variant for the product, Shopify creates one under the hood.

To answer your questions,

  1. There is an options array that specifies the different options for variants but you cannot specify an option value and not have any variant utilise that option value; any such values without an associated variant would be ignored. Therefore, the values specified for an option are actually ignored, because it is populated using the variant options. If you take a look at the section to add a variant on the Admin store, the values for the options are not presented as a drop down, however, a drop down is available on the customer-facing storefront. In essence, if you want a value to be part of the options array, then a variant has to exist that specifies a value for that option. If the variant isn't available, then you can set its quantity to zero.
  2. To create additional variants, you would need the Product Variant resource  to do this.

If you had any additional questions, let me know.

Best,
Seth.

syf_ | Developer Support @ 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