In order to create configurable product I need to create configurable product, virtual product and finally connect them.
Example of json request got from here: How do I create a configurable product using the REST API v2?
I am wondering why do I need this section below in configurable product?
"configurable_product_options":[
{
"attribute__id":"193",
"label":"Colour",
"position":0,
"values":[
{
"value_index":340
},
{
"value_index":341
}
],
I noticed that this section is required in able to connect virtual product to configurable connect later. But values has no meaning.
In virtual product I can assign any value I want. What is the purpose of this values?
2 Answers 2
You can use this example to create a configurable product.
{
"product": {
"sku": "MS-Lime",
"name": "Lime Tee",
"attribute_set_id": 9,
"status": 1,
"visibility": 4,
"type_id": "configurable",
"weight": "0.6",
"extension_attributes": {
"category_links": [
{
"position": 0,
"category_id": "11"
},
{
"position": 1,
"category_id": "12"
},
{
"position": 2,
"category_id": "16"
}
]
},
"custom_attributes": [
{
"attribute_code": "description",
"value": "The Dime Tee is soft, comfortable and durable. You will love the way you look in this tailored tee shirt."
},
{
"attribute_code": "tax_class_id",
"value": "2"
},
{
"attribute_code": "material",
"value": "148"
},
{
"attribute_code": "pattern",
"value": "196"
},
{
"attribute_code": "color",
"value": "52"
}
]
}
}
As you can see I am not using
configurable_product_options
in my body. This is not the mandatory field. You can totally skip this.
Please try with below code, I hope its work for you.
created a simple product with 'color' attribute and simple product ids are 1011,1012 & 1013.
<?php
/********* Create Configurable Product By Rest API *********/
try {
$url = "http://siteurl.com";
$apiusername = 'apiusername';
$apipassword = 'apipassword';
$userData = array("username" => $apiusername, "password" => $apipassword);
$ch = curl_init($url."/rest/V1/integration/admin/token");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($userData));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Content-Lenght: " . strlen(json_encode($userData))));
$token = curl_exec($ch);
$product_data= '{
"product": {
"id": 0,
"sku": "config_1",
"name": "Config Product",
"attributeSetId": 4,
"price": 20,
"status": 1,
"visibility": 4,
"typeId": "configurable",
"createdAt": "string",
"updatedAt": "string",
"weight": 0.8,
"extensionAttributes": {
"stockItem": {
"isInStock": true
},
"configurableProductLinks": [1011,1012,1013],
"configurableProductOptions": [
{
"id": 0,
"attributeId": "93",
"label": "Color",
"position": 0,
"isUseDefault": true,
"values": [
{
"valueIndex": 11
},
{
"valueIndex": 12
},
{
"valueIndex": 13
}
]
}
]
}
}
}';
$ch = curl_init($url."/rest/V1/products");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$product_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json", "Authorization: Bearer " . json_decode($token)));
$result = curl_exec($ch);
}catch(Exception $e){
echo $e->getMessage();
}
var_dump($result);
?>
Explore related questions
See similar questions with these tags.