I'm new to the Magento 2 REST API (this is also my first post in this forum, hi all), but during the past day have been able to retrieve information using it to my backend system. GET -requests are easy enough, but I'm struggling with getting a correct PUT request together.
What I need to do next is to update the order status from pending to processing, but just cannot figure out how to create a proper array to encode. I've endlessly Googled this up to the point where I've all but given up. There are a million examples on how to retrieve information from Magento, but pretty much NOTHING on how to make updates.
Here's my code:
(authentication part omitted - that works great)
$icrement_id = "000000003";
$requestUrl='http://www.example.com/rest/V1/orders?searchCriteria[filter_groups][0][filters][0][field]=increment_id&searchCriteria[filter_groups][0][filters][0][value]='.$increment_id;
$ch = curl_init($requestUrl);
// Set status to processing
$data_json = [
"items"=> [
"state"=> "processing"
]
];
$data_json = json_encode($data);
print '<br>JSON data: '.$data_json;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', $headers, 'Content-Length: ' . strlen($data_json)));
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
curl_setopt($ch, CURLOPT_POSTFIELDS,$data_json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
// Execute the query
$result = curl_exec($ch);
// Decode the result
$result= json_decode($result);
print_r($result);
Questions:
- How to formulate $data, so that it would be correct (items[0] -> status from pending to processing)
- Is my PUT request correct otherwise? (f.ex. can I use a search as the requestURL)
So just to be crystal clear, I'm not trying to create a new order, but change an existing order. Examples that would give a 'for dummies' example of a REST API PUT -request would be appreciated also. Thanks.