更新订单 WooCommerce API PHP CURL.


Updating order WooCommerce API PHP CURL

我正在为我的同事开发一个Web应用程序,所以他们将有一个带有应用程序的iPad来打包我们的订单。

整个应用程序几乎完成,最后一步是将订单状态从处理更改为已完成

我在做什么:

获取当前订单 ID,然后使用 curl 或 API 更改状态。发生的事情很奇怪,我取回了 JSON 数据,completed_at时间更新了,但状态仍在处理中。

下面你会看到我的代码:

$data = array("status" => "completed");                                                                    
$data_string = json_encode($data);                                                                                   
$username = 'XXX'; // Add your own Consumer Key here
$password = 'XXX'; // Add your own Consumer Secret here
$ch = curl_init('https://www.deallerleukste.nl/wc-api/v2/orders/5764?consumer_key='.$username.'&consumer_secret='.$password);
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);                                  
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");                                                                     
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);                                                                     
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
    'Content-Type: application/json',                                                                                
    'Content-Length: ' . strlen($data_string))                                                                       
);                                                                                                                   
$result = curl_exec($ch);
echo $result;

有人看到我做错了什么吗?请帮忙!

问候

杰尔默

尝试使用此代码

$data='{  "order": {    "status": "completed"  }}';

动态

$data=json_encode( array( 'order' => array( 'status' => $status)));

在$status中,您可以传递所需的状态。

我看到这是一个很旧的帖子,但今天我遇到了同样的问题,所以也许它很有用。

您必须在此处使用 PUT 而不是 POST:

curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");

并且数据应为以下格式的有效 JSON:

$data='{"status": "completed"}';