Magento 2 Rest API订单编辑

问题描述 投票:0回答:1

我正在试图弄清楚如何在我请求之后编辑订单。无论是否导出订单,我都会创建自定义属性。

我首先获得状态未导出的所有订单,在我导出后,我想将自定义属性更改为导出。

编辑/更新订单的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,
rest magento2 guzzle
1个回答
1
投票

要编辑/更新订单详细信息,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创建新订单。

© www.soinside.com 2019 - 2024. All rights reserved.