First thing I would like to share is that I am very new to magento. I am working on addition of products using rest api. I tried one product and it was created.
$headers = array('Authorization=> Bearer '. $token1);
$requestUrl='http://localhost/development/index.php/rest/V1/products';
//Use above token into header
//Please note 24-MB01 is sku
$ch = curl_init();
$ch = curl_init($requestUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
$post ='{
"product": {
"sku": "MY_SKU11",
"name": "My Product223",
"attribute_set_id": "4",
"price": 110,
"status": 1,
"visibility": 4,
"typeId": "simple",
"weight": 10,
"extensionAttributes": {
"stockItem": {
"stockId": 1,
"qty": 20,
"isInStock": true,
"isQtyDecimal": false,
"useConfigMinQty": true,
"minQty": 0,
"useConfigMinSaleQty": 0,
"minSaleQty": 0,
"useConfigMaxSaleQty": true,
"maxSaleQty": 0,
"useConfigBackorders": false,
"backorders": 0,
"useConfigNotifyStockQty": true,
"notifyStockQty": 20,
"useConfigQtyIncrements": false,
"qtyIncrements": 0,
"useConfigEnableQtyInc": false,
"enableQtyIncrements": false,
"useConfigManageStock": true,
"manageStock": true,
"lowStockDate": "string",
"isDecimalDivided": true,
"stockStatusChangedAuto": 0,
"extensionAttributes": {}
}
},
"options": [],
"tierPrices": [],
"customAttributes": [
]
},
"saveOptions": true
}';
$options = array(
CURLOPT_URL=>$requestUrl,
CURLOPT_VERBOSE=>0,
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_USERAGENT=>"Mozilla/4.0 (compatible;)",
CURLOPT_POST=>true,
CURLOPT_POSTFIELDS=>$post,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "PUT",
CURLOPT_HTTPHEADER => array(
"authorization: Bearer $token1",
"content-type: application/json"
),
);
curl_setopt_array($ch, $options);
$result = curl_exec($ch);
$result= json_decode($result);
echo '</br>Response for add product<pre>';
print_r($result);
The product is being created but when I am trying to update the same product and want to change price. I am unable to do so.
Can you guys help me out in this? Also is there any api documentation for this?
Thanks
2 Answers 2
I'm pretty sure I had the same problem. Try and change the route to: /all/V1/products/{sku}
"all" applies to All Store views. Price and weight are global attributes and therefore not changed because /V1/products/{sku} only updates a store view.
I have not found any documentation on this topic...
-
1Thanks Casper.. Passing all in the api parameter worked for me. You saved my day man.Md Jawed Shamshedi– Md Jawed Shamshedi2017年03月15日 10:23:12 +00:00Commented Mar 15, 2017 at 10:23
-
Good to hear. Please mark my answer as the solution.Casper Skovgaard– Casper Skovgaard2017年03月16日 08:39:34 +00:00Commented Mar 16, 2017 at 8:39
-
Thanks! For more documentation about it : devdocs.magento.com/guides/v2.1/rest/generate-local.htmlDevonDahon– DevonDahon2017年06月24日 14:49:28 +00:00Commented Jun 24, 2017 at 14:49
-
is there a way to pass
store_idwith Rest apiMahendra Jella– Mahendra Jella2017年08月22日 07:23:21 +00:00Commented Aug 22, 2017 at 7:23 -
@MahendraJella replace "all" with store_code ( <store_code>/schema)Casper Skovgaard– Casper Skovgaard2017年08月23日 08:38:45 +00:00Commented Aug 23, 2017 at 8:38
You should use PUT: /V1/products/{sku}.
For example:
$productData = [
"sku" : "sku-test"
"name": "Test",
"price": 100,
"status": 1
];
We can see more here: https://magento.stackexchange.com/a/151342/33057
-
yes I am doing the same. Name, status and other things are updating but price and weight are not updating.Md Jawed Shamshedi– Md Jawed Shamshedi2017年03月14日 14:21:01 +00:00Commented Mar 14, 2017 at 14:21
-
I will check again! Which is your Magento version?Khoa Truong– Khoa Truong2017年03月14日 14:25:10 +00:00Commented Mar 14, 2017 at 14:25