Create product with multiple variants having 2 options via API.

Aniket546
New Member
3 0 1

Hi Everyone, I hope somebody can help me with this one.

I have to create a product with multiple variants having 2 options (size,Color). Here is my code 

$products_array = array(
"product"=>array(
"title"=> "Test Product",
"body_html"=> "<strong>Test Product Creation!<\/strong>",
"vendor"=> "Vendor",
"product_type"=> "Test",
"published"=> false,
"variants"=>array(
array(
"sku"=>"100001-100001-c12-L",
"price"=>20.00,
"required_shipping" => true,
"option1" => "L",
"option2"=>"Red"
),
array(
"sku"=>"100001-100001-c14-S",
"price"=>20.00,
"required_shipping" => true,
"option1" => "Small",
"option2" => "Green"
)),
"options"=>array(
"Size","Color" )
)
);

And getting this error message
{"errors":{"options":["is invalid"],"name":["can't be blank"]}}

Can anyone please help me to create a product with multiple variants.
 
Thanks in advance 

 

FahadMehmood
New Member
1 0 0

@Aniket546 You are missing these parameters. Look at this Create Product  with multiple variants ( color, size) JSON.

{

  "product": {
    "title": "Product With Two Variants",
    "body_html": "<strong>Good snowboard!</strong>",
    "vendor": "fMboys",
    "product_type": "Snowboard",
    "variants": [
      {
        "option1": "Blue",
        "option2": "S"
      },
      {
        "option1": "Black",
        "option2": "L"
      }
    ],
    "options": [
      {
        "name": "Color",
        "values": [
          "Blue",
          "Black"
        ]
      },
      {
        "name": "Size",
        "values": [
          "S",
          "L"
        ]
      }
    ]
  }
}
 
The code I marked as Bold is the part that you were missed.
Hoping you got it.
0 Likes