Update Product Using REST API in Magento2

apimagento2productrest

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

Best Answer

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