5

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

Khoa Truong
32.5k11 gold badges91 silver badges159 bronze badges
asked Mar 14, 2017 at 14:02

2 Answers 2

14

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...

answered Mar 14, 2017 at 18:35
6
  • 1
    Thanks Casper.. Passing all in the api parameter worked for me. You saved my day man. Commented Mar 15, 2017 at 10:23
  • Good to hear. Please mark my answer as the solution. Commented Mar 16, 2017 at 8:39
  • Thanks! For more documentation about it : devdocs.magento.com/guides/v2.1/rest/generate-local.html Commented Jun 24, 2017 at 14:49
  • is there a way to pass store_id with Rest api Commented Aug 22, 2017 at 7:23
  • @MahendraJella replace "all" with store_code ( <store_code>/schema) Commented Aug 23, 2017 at 8:38
2

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

answered Mar 14, 2017 at 14:16
2
  • yes I am doing the same. Name, status and other things are updating but price and weight are not updating. Commented Mar 14, 2017 at 14:21
  • I will check again! Which is your Magento version? Commented Mar 14, 2017 at 14:25

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.