1

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:

  1. How to formulate $data, so that it would be correct (items[0] -> status from pending to processing)
  2. 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.

0

As per the documentation in Magento Swagger Api you should use http://www.example.com/rest/V1/orders API with POST request to update order details. Here is the curl request example:

$entity_id = 3;
$request_url='http://www.example.com/rest/V1/orders';
$data_json = [
    "entity"=> [
        "entity_id" => $entity_id,
        "state" => "processing",
        "status" => "Processing"
    ]
];
$data_string = json_encode($data_json);
$ch = curl_init($request_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-Type: application/json',
    'Content-Length: ' . strlen($data_string))
);
$response = curl_exec($ch);
$response =  json_decode($response);
print_r($response);
curl_close($ch);

In case you want to update the order details using increment_id, than you will have to create your own API which accepts the increment_id as input for order detail updates. One of the example for cutom API is demonstrated here

  • This worked perfectly, thank you. The order ID seems to update every time I make a change, as in order #000000003 first becomes #000000004, then #000000005 etc. Is there any way to stop this behavior? – Mr Panther Jan 6 '18 at 15:31
  • Welcome!! Generally this does not happen, unless you are setting it forcefully. Just you need to check the entity_id is correct or not. And if you are submitting the increment_id in POST make sure it is same as that of the order for which entity_id is. If all this does not fix you problem please let me know the exact version no of Magento 2 you are using. So, that I can check on it. – Kamal Singh Jan 7 '18 at 1:08
  • @Mr Panther, increment_id happens to update because you have to declare it on post param as string not as int type. – fmsthird Feb 4 at 11:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.