php - 根据 Woocommerce 3 中的外部服务响应更新订单状态
问题描述
如果外部配送服务状态为“订单完成”,则在 woocommerce 中更新为“已完成”。如果外部配送服务状态为“订单已取消”,则在 woocommerce 中更新为“已取消”。
需要“更新订单状态”部分。
function get_order_ids_to_check(){
global $wpdb;
return $wpdb->get_col( "
SELECT p.ID
FROM {$wpdb->prefix}posts as p
WHERE p.post_type LIKE 'shop_order'
AND p.post_status IN ('wc-on-hold','wc-processing')
AND UNIX_TIMESTAMP(p.post_date) >= (UNIX_TIMESTAMP(NOW()) - 86400)
" );
// Get the Orders IDs to check
$orders_ids = get_order_ids_to_check();
// Loop through each order Ids
foreach( $orders_ids as $order_id ){
// Get the delivery order ID related to your external shipping service
$delivery_order_id = get_post_meta( $order_id, 'delivery_order_id', true );
$param['secret'] = "";
$param['order_id'] = $delivery_order_id;
foreach ($param as $key => $value) {
$data .= "&".$key."=".$value;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://app.example.com/api/index.php?get_status");
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
$decoded = (array) json_decode($result);
// Update order status
if( isset($decoded['result']) && $decoded['result'] == 'success' && isset($decoded['status']) && !empty($decoded['status']) ){
// If the received order status from the external delivery service is "Order completed", then change it in woocommerce "Completed"
}
}
}
解决方案
要更新订单状态,您将使用WC_Order
方法update_status()
。
我已经重新访问了您的代码。尝试以下操作:
function get_order_ids_to_check(){
global $wpdb;
return $wpdb->get_col( "
SELECT p.ID
FROM {$wpdb->prefix}posts as p
WHERE p.post_type LIKE 'shop_order'
AND p.post_status IN ('wc-on-hold','wc-processing')
AND UNIX_TIMESTAMP(p.post_date) >= (UNIX_TIMESTAMP(NOW()) - 86400)
" );
}
function send_daily_orders_to_delivery(){
// Loop through each order Ids
foreach( get_order_ids_to_check() as $order_id ){
// Get an instance of the WC_Order object
$order = wc_get_order($order_id);
$secret = ''; // <== Secret key to be set
$data = '&secret='.$secret.'&order_id='.get_post_meta( $order_id, 'delivery_order_id', true );
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://app.example.com/api/index.php?get_status");
curl_setopt($ch, CURLOPT_FAILONERROR, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
$decoded = (array) json_decode($result);
// Update order status
if( isset($decoded['result']) && $decoded['result'] == 'success' && isset($decoded['status']) && ! empty($decoded['status']) ){
if( $decoded['status'] == "Order Completed" )
$order->update_status( 'completed' );
elseif( $decoded['status'] == "Order Cancelled" )
$order->update_status( 'cancelled' );
}
}
}
然后你将使用:
send_daily_orders_to_delivery();
在调度程序函数或您选择的任何操作挂钩中。
代码位于活动子主题(或活动主题)的 function.php 文件中。它应该工作。
推荐阅读
- powerbi - 根据 Power bi 本身中的特定表连接三个表
- php - 如何在 HTML 表单上显示 PHP 从数据库中获取数据?
- java - 使用 JOOQ 创建动态主键约束
- java - Eclipse IDE 中的设计选项卡为空白
- android - android将recyclerview项目位置传递给对话框按钮监听器
- reactjs - 在 Reactjs 中使用 map 复制对象
- javascript - 使用 Google Cloud Functions 抓取时 page.evaluate 不返回响应?
- javascript - 开玩笑抛出错误 SyntaxError: Unexpected identifier
- javascript - React.js 如何在组件内循环和渲染组件?
- php - 如何使 mail() 消息间距