请尝试以下代码,希望它对您有用。
创建了具有“颜色”属性的简单产品,简单产品ID为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);
?>