1

I want to create configurable product and its related simple products at the same time via API.

For example: shoes; with different sizes(attribute=shoesize).. as like:

Prada's new shoe (configurable product)... And it's child sizes: 36 => stock: 4 (simple product), 37 => stock: 2(simple product) etc..

I'm recently using the code at below, to add simple products; could someone help me to update my code to resolve my need?

<?php
$url = "http://www.example.com/";
$token_url=$url."rest/V1/integration/admin/token";
$product_url=$url. "rest/V1/products";
$ch = curl_init();
$data = array("username" => username, "password" => password);
$data_string = json_encode($data);
$ch = curl_init($token_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$token = curl_exec($ch);
$adminToken=  json_decode($token);

$sampleProductData = array(
'sku'               => 'SAMPLESKU',
'name'              => 'sample product',
'visibility'        => 4,
'type_id'           => 'simple',
'price'             => '200',
'status'            => 1,
'attribute_set_id'  => 101,
'weight'            => 1,
'manufacturer'            => 25,
'extension_attributes' => array(
        "stock_item"=>array(
                'qty' => $inventory_stock,'is_in_stock' => 1,'manage_stock' => 1,'use_config_manage_stock' => 1,'min_qty' => 0,'use_config_min_qty' => 1,'min_sale_qty' => 1,'use_config_min_sale_qty' => 1,'max_sale_qty ' => 10,'use_config_max_sale_qty' => 1,'is_qty_decimal' => 0,'backorders' => 0,'use_config_backorders' => 1,'notify_stock_qty' => 1,'use_config_notify_stock_qty' => 1
        ),
),
'custom_attributes' => array(
    array( 'attribute_code' => 'category_ids', 'value' => ["6"] ),
    array( 'attribute_code' => 'description', 'value' => 'xxxxx' ),
    array( 'attribute_code' => 'short_description', 'value' => 'xxxxx' ),
    array( 'attribute_code' => 'meta_title', 'value' => 'xxxxx'),
    array( 'attribute_code' => 'meta_keyword', 'value' => 'xxxxx'),
    array( 'attribute_code' => 'meta_description', 'value' => 'xxxxx'),
),




);

$productData = json_encode(array('product' => $sampleProductData));

$setHaders = array('Content-    Type:application/json','Authorization:Bearer '.$adminToken);

$ch = curl_init();
curl_setopt($ch,CURLOPT_URL, $product_url);
curl_setopt($ch,CURLOPT_POSTFIELDS, $productData);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, $setHaders);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
$response=  json_decode($response);
echo $response;
curl_close($ch);

?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.