Create product using SOAP API and JSON input
-
29-09-2020 - |
Question
I need to create product with SOAP API
and JSON
, here is my JSON
input:
{"sku":"tsett","name":"TEST","weight":"3.0000","qty":"18.0000","brand":"sample","category_ids":["107","108","109"],"pirce":"Rs. 1,500.00","description ":"sample disc"}
here is the create product api code, how to add the json request using values of request to create the product:
$client = new SoapClient('http://magentohost/api/soap/?wsdl');
$session = $client->login('apiUser', 'apiKey');
$attributeSets = $client->call($session, 'product_attribute_set.list');
$attributeSet = current($attributeSets);
$result = $client->call($session, 'catalog_product.create', array('simple', $attributeSet['set_id'], 'product_sku', array(
'categories' => array(2),
'websites' => array(1),
'name' => 'Product name',
'description' => 'Product description',
'short_description' => 'Product short description',
'weight' => '10',
'status' => '1',
'url_key' => 'product-url-key',
'url_path' => 'product-url-path',
'visibility' => '4',
'price' => '100',
'tax_class_id' => 1,
'meta_title' => 'Product meta title',
'meta_keyword' => 'Product meta keyword',
'meta_description' => 'Product meta description'
)));
var_dump ($result);
Solution
Decode JSON data and assign values as per requirement
$myJsonData = '{"sku":"tsett","name":"TEST","weight":"3.0000","qty":"18.0000","brand":"sample","category_ids":["107","108","109"],"pirce":"Rs. 1,500.00","description":"sample disc"}';
$myJsonData= json_decode($myJsonData,1);
$client = new SoapClient('http://magentohost/api/soap/?wsdl');
$session = $client->login('apiUser', 'apiKey');
$attributeSets = $client->call($session, 'product_attribute_set.list');
$attributeSet = current($attributeSets);
$result = $client->call($session, 'catalog_product.create', array('simple', $attributeSet['set_id'], $myJsonData['sku'], array(
'categories' => array_values($myJsonData['category_ids']),
'websites' => array(1),
'name' => $myJsonData['name'],
'description' => $myJsonData['description'],
'short_description' => 'Product short description',
'weight' => $myJsonData['weight'],
'status' => '1',
'url_key' => 'product-url-key',
'url_path' => 'product-url-path',
'visibility' => '4',
'price' => $myJsonData['pirce'],
'tax_class_id' => 1,
'meta_title' => 'Product meta title',
'meta_keyword' => 'Product meta keyword',
'meta_description' => 'Product meta description'
)));
var_dump ($result);
UPDATE
For multiple product as per your request
$myJsonData = '{"products":[{"sku":"tsett","name":"TEST","weight":"3.0000","qty":"18.0000","brand":"sample","category_ids":["107","108","109"],"pirce":"Rs. 1,500.00"},{"sku":"product_sku","name":"Product name","weight":"10.0000","qty":"0.0000","brand":null,"category_ids":["2"],"pirce":"Rs. 100.00"}]}';
$myJsonData = json_decode($myJsonData,1);
foreach($myJsonData['products'] as $_product) {
$result = $client->call($session, 'catalog_product.create', array('simple', $attributeSet['set_id'], $_product['sku'], array(
'categories' => array_values($_product['category_ids']),
'websites' => array(1),
'name' => $_product['name'],
'description' => $_product['description'],
'short_description' => 'Product short description',
'weight' => $_product['weight'],
'status' => '1',
'url_key' => 'product-url-key',
'url_path' => 'product-url-path',
'visibility' => '4',
'price' => $_product['pirce'],
'tax_class_id' => 1,
'meta_title' => 'Product meta title',
'meta_keyword' => 'Product meta keyword',
'meta_description' => 'Product meta description'
)));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange