2017-06-08 41 views
0

我試圖弄清楚如何在我請求後編輯訂單。 我做了一個自定義屬性,無論是否導出訂單。Magento 2 Rest API訂單編輯

我首先獲取所有狀態未導出的訂單,並在導出它們之後,我想將自定義屬性更改爲導出。

什麼是REST請求編輯/更新訂單?我不斷收到類似的錯誤消息:

{"message":"%fieldName is a required field.","parameters": 
{"fieldName":"entity"} 

這是我的代碼:

$json = array(
     "entity_id" => $id, 
     "extension_attributes" => array(
      "custom_export_attribute" => "exported", 
      ) 
     ); 
    $webapi = new ApiClient('https://dev.local.nl', self::$username, self::$password); 
    $response = $webapi->getClient()->request('PUT', '/rest/V1/orders/create', [ 

     'headers' => [     
      'Authorization'    => "Bearer " . $webapi->getToken(), 
      'Content-Type'    => "application/json" 
     ], 
     'body'  => json_encode($json) 

    ]);  
    return json_decode($response->getBody(), true); 

我也試過:

$webapi->getClient()->request('PUT', '/rest/V1/orders/'.$id, 

回答

0

編輯/更新訂單詳細信息,Magento的2 /V1/orders接受POST請求方法。按照Magento 2 Dev Doc,它接受請求主體如下格式(您可以在文檔頁面整體JSON請求):

{ 
    "entity": { 
     "entity_id": 0, 
     "extension_attributes": { 

     } 
    } 
} 

所以,你只需要更新$json變量:

$json = [ 
    "entity"=> [ 
     "entity_id" => $id, 
     "extension_attributes" => [ 
      "custom_export_attribute" => "exported" 
     ] 
    ] 
] 

而不是調用POST請求方法而不是PUT。在我的建議中,更願意使用Create API創建新訂單。