Add options to Product Variant - REST API

Solved
helidona
Excursionist
19 0 4

Hi, 


I'm having a problem using a REST API's endpoint 

POST /admin/api/products.json

While I insert a new product with variant and add variant options. Here is an example:

{
    "product": {
        "title": "Kapuzenpulli - Pusteblume",
        "vendor": "Super-Fashion",
        "published": "1",
        "variants": [
            {
                "sku": "D1030Q02024T_middle-green_L",
                "title": "Farbe: middle-green, Größe: L",
                "price": "34.95",
                "presentment_prices": {
                    "compare_at_price": {
                        "amount": "34.95",
                        "currency_code": "EUR"
                    }
                },
                "weight": "700",
                "weight_unit": "",
                "option1": "middle-green",
                "option2": "L",
                "option3": "Farbe: middle-green, Größe: L",
            },
            {
                "sku": "D1030Q02024T_middle-green_M",
                "title": "Farbe: middle-green, Größe: M",
                "price": "34.95",
                "presentment_prices": {
                    "compare_at_price": {
                        "amount": "34.95",
                        "currency_code": "EUR"
                    }
                },
                "weight": "700",
                "weight_unit": "",
                "option1": "middle-green",
                "option2": "M",
                "option3": "Farbe: middle-green, Größe: M",
            },
            {
                "sku": "D1030Q02024T_middle-green_S",
                "title": "Farbe: middle-green, Größe: S",
                "price": "34.95",
                "presentment_prices": {
                    "compare_at_price": {
                        "amount": "34.95",
                        "currency_code": "EUR"
                    }
                },
                "weight": "700",
                "weight_unit": "",
                "option1": "middle-green",
                "option2": "S",
                "option3": "Farbe: middle-green, Größe: S",
            },
            {
                "sku": "D1030Q02024T_middle-green_XL",
                "title": "Farbe: middle-green, Größe: XL",
                "price": "34.95",
                "presentment_prices": {
                    "compare_at_price": {
                        "amount": "34.95",
                        "currency_code": "EUR"
                    }
                },
                "weight": "700",
                "weight_unit": "",
                "option1": "middle-green",
                "option2": "XL",
                "option3": "Farbe: middle-green, Größe: XL",
            },
            {
                "sku": "D1030Q02024T_dark-rose_M",
                "title": "Farbe: dark-rose, Größe: M",
                "price": "34.95",
                "presentment_prices": {
                    "compare_at_price": {
                        "amount": "34.95",
                        "currency_code": "EUR"
                    }
                },
                "weight": "700",
                "weight_unit": "",
                "option1": "dark-rose",
                "option2": "M",
                "option3": "Farbe: dark-rose, Größe: M",
            },
            {
                "sku": "D1030Q02024T_dark-rose_S",
                "title": "Farbe: dark-rose, Größe: S",
                "price": "34.95",
                "presentment_prices": {
                    "compare_at_price": {
                        "amount": "34.95",
                        "currency_code": "EUR"
                    }
                },
                "weight": "700",
                "weight_unit": "",
                "option1": "dark-rose",
                "option2": "S",
                "option3": "Farbe: dark-rose, Größe: S",
            },
            {
                "sku": "D1030Q02024T_middle-grey_XL",
                "title": "Farbe: middle-grey, Größe: XL",
                "price": "34.95",
                "presentment_prices": {
                    "compare_at_price": {
                        "amount": "34.95",
                        "currency_code": "EUR"
                    }
                },
                "weight": "700",
                "weight_unit": "",
                "option1": "middle-grey",
                "option2": "XL",
                "option3": "Farbe: middle-grey, Größe: XL",
            }
        ],
        "tags": "Bio glutenfrei Peanut - erdnussriegel break schnitzer erdnuss riegel müsliriegel müsli riegel"
    }
}

API RESPONSE:
Error: The variant 'middle-green' already exists.

I don't know if I am using the correct way to add options, or exist any other way.

Looking forward to your help and advice!

Regards!

0 Likes
Jayvin
Shopify Partner
182 28 43

Hi Helidona,

I encountered this issue in the past. As a quick workaround, create the product first using the example json below:

{
  "product": {
    "title": "Kapuzenpulli - Pusteblume",
    "vendor": "Super-Fashion",
    "published": "1",
    "tags": "Bio glutenfrei Peanut - erdnussriegel break schnitzer erdnuss riegel müsliriegel müsli riegel",
    "options": [
      {
        "name": "Color",
        "values": [
          "middle-green",
          "dark-rose",
          "middle-grey"
        ]
      },
      {
        "name": "Size",
        "values": [
          "S",
          "M",
          "L",
          "XL"
        ]
      },
      {
        "name": "YOUR OPTION 3 NAME",
        "values": [
          "Farbe: middle-green, Größe: L",
          "Farbe: middle-green, Größe: M",
          "Farbe: middle-green, Größe: S",
          "Farbe: middle-green, Größe: XL",
          "Farbe: dark-rose, Größe: M",
          "Farbe: dark-rose, Größe: S",
          "Farbe: middle-grey, Größe: XL"
        ]
      }
    ]
  }
}

After the product has been created, you can then add the variants one by one using the add variant endpoint.

helidona
Excursionist
19 0 4

Hi, 

I will do in this way!!

Thank you for your help :)

helidona
Excursionist
19 0 4

Hi @Jayvin,


I tried to created a product with the above solution but I'm having Size issue:
Here is the code

{
    "product": {
        "title": "Kapuzenpulli - Pusteblume",
        "vendor": "Super-Fashion",
        "published": "1",
         "tags": "Bio glutenfrei Peanut - erdnussriegel break schnitzer erdnuss riegel müsliriegel müsli riegel",
        "options": [
            {
                "name": "Color",
                "values": [
                    "middle-green",
                    "dark-rose",
                    "middle-grey"
                ]
            },
            {
                "name": "Size",
                "values": [
                    "L",
                    "M",
                    "S",
                    "XL"
                ]
            }
        ]
    }
}


And I got this error after I send a request to Shopify ADMIN API

"base": [
"You need to add option values for Size"
]
Furthermore, I changed the Size values to numbers like ["42","43","44","45"] and it is same issue!!

Looking forward to your help!
Jayvin
Shopify Partner
182 28 43

This is an accepted solution.

Hi @helidona 

OK here is the tested solution to create products and variants in a single call.

 

{
   "product":{
      "title":"Kapuzenpulli - Pusteblume",
      "vendor":"Super-Fashion",
      "published":"1",
      "options":[
         {
            "name":"Color",
            "position":0,
            "values":[
               "middle-green",
               "dark-rose",
               "middle-grey"
            ]
         },
         {
            "name":"Size",
            "position":1,
            "values":[
               "S",
               "M",
               "L",
               "XL"
            ]
         },
         {
            "name":"Farbe",
            "position":2,
            "values":[
               "Farbe: middle-green, Größe: L",
               "Farbe: middle-green, Größe: M",
               "Farbe: middle-green, Größe: S",
               "Farbe: middle-green, Größe: XL",
               "Farbe: dark-rose, Größe: M",
               "Farbe: dark-rose, Größe: S",
               "Farbe: middle-grey, Größe: XL"
            ]
         }
      ],
      "variants":[
         {
            "sku":"D1030Q02024T_middle-green_L",
            "title":"Farbe: middle-green, Größe: L",
            "price":"34.95",
            "presentment_prices":{
               "compare_at_price":{
                  "amount":"34.95",
                  "currency_code":"EUR"
               }
            },
            "weight":"700",
            "weight_unit":"",
            "option1":"middle-green",
            "option2":"L",
            "option3":"Farbe: middle-green, Größe: L"
         },
         {
            "sku":"D1030Q02024T_middle-green_M",
            "title":"Farbe: middle-green, Größe: M",
            "price":"34.95",
            "presentment_prices":{
               "compare_at_price":{
                  "amount":"34.95",
                  "currency_code":"EUR"
               }
            },
            "weight":"700",
            "weight_unit":"",
            "option1":"middle-green",
            "option2":"M",
            "option3":"Farbe: middle-green, Größe: M"
         },
         {
            "sku":"D1030Q02024T_middle-green_S",
            "title":"Farbe: middle-green, Größe: S",
            "price":"34.95",
            "presentment_prices":{
               "compare_at_price":{
                  "amount":"34.95",
                  "currency_code":"EUR"
               }
            },
            "weight":"700",
            "weight_unit":"",
            "option1":"middle-green",
            "option2":"S",
            "option3":"Farbe: middle-green, Größe: S"
         },
         {
            "sku":"D1030Q02024T_middle-green_XL",
            "title":"Farbe: middle-green, Größe: XL",
            "price":"34.95",
            "presentment_prices":{
               "compare_at_price":{
                  "amount":"34.95",
                  "currency_code":"EUR"
               }
            },
            "weight":"700",
            "weight_unit":"",
            "option1":"middle-green",
            "option2":"XL",
            "option3":"Farbe: middle-green, Größe: XL"
         },
         {
            "sku":"D1030Q02024T_dark-rose_M",
            "title":"Farbe: dark-rose, Größe: M",
            "price":"34.95",
            "presentment_prices":{
               "compare_at_price":{
                  "amount":"34.95",
                  "currency_code":"EUR"
               }
            },
            "weight":"700",
            "weight_unit":"",
            "option1":"dark-rose",
            "option2":"M",
            "option3":"Farbe: dark-rose, Größe: M"
         },
         {
            "sku":"D1030Q02024T_dark-rose_S",
            "title":"Farbe: dark-rose, Größe: S",
            "price":"34.95",
            "presentment_prices":{
               "compare_at_price":{
                  "amount":"34.95",
                  "currency_code":"EUR"
               }
            },
            "weight":"700",
            "weight_unit":"",
            "option1":"dark-rose",
            "option2":"S",
            "option3":"Farbe: dark-rose, Größe: S"
         },
         {
            "sku":"D1030Q02024T_middle-grey_XL",
            "title":"Farbe: middle-grey, Größe: XL",
            "price":"34.95",
            "presentment_prices":{
               "compare_at_price":{
                  "amount":"34.95",
                  "currency_code":"EUR"
               }
            },
            "weight":"700",
            "weight_unit":"",
            "option1":"middle-grey",
            "option2":"XL",
            "option3":"Farbe: middle-grey, Größe: XL"
         }
      ],
      "tags":"Bio glutenfrei Peanut - erdnussriegel break schnitzer erdnuss riegel müsliriegel müsli riegel"
   }
}

Rename your option 3 accordingly. options property was missing in your initial call.

helidona
Excursionist
19 0 4

HI @Jayvin,

thanks for your efforts it is really appreciated!

Meantime, I used another format with GraphQL mutations, and it worked!

Example:

mutation {
  productCreate(input: {
         bodyHtml:"dddd",
         descriptionHtml:"eeee",
         tags:"dddd, eee,ggg",
         title:"Tested",
         vendor:"Burton",
         options: ["Size", "Color"],
         variants: [
            {
            price:"111",
            weight:1,
            sku: "4444444",
            options: ["S", "Black"]
            },
            {
            price:"222",
            weight:1.3,
            sku: "bbb",
            options: ["S", "Blue"]
            },
            {
            price:"333",
            weight:1.3,
            sku: "tttt",
            options: ["M", "Pink"]
            }
        ]
         published: true
	}
  ) {
    product {
      id
    }
    userErrors {
      field
      message
    }
  }
}