php - 根据 Woocommerce 3 中的订单状态将订单发送到送货服务
问题描述
我的订单会自动发送到外部送货服务。但是当订单状态发生变化时,它会再次被发送到外部交付服务。如何解决?
add_action( 'woocommerce_order_status_changed', 'order_stock_reduction_based_on_status', 20, 4 );
function order_stock_reduction_based_on_status( $order_id, $old_status, $new_status, $order ){
// Only for 'processing' and 'hold-on' order statuses change
if ( $new_status == 'processing' || $new_status == 'hold-on' ) {
// Checking if this has already been done avoiding reload
if (get_post_meta($order_id, 'delivery_order_id', true)) {
return; // Exit if already processed
}
}
$order_data = $order->get_data();
// Send data
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://app.example.com/api/index.php?new_order");
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_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
$decoded = (array) json_decode($result);
// Output
if( isset($decoded['result']) && $decoded['result'] == 'success' && isset($decoded['order_id']) && !empty($decoded['order_id']) ){
update_post_meta( $order_id, 'delivery_order_id', esc_attr( $decoded['order_id'] ) );
}
}
解决方案
我认为问题很简单。第一条IF
语句应该在最后关闭,以避免这种重复问题。
所以你的代码将是:
add_action( 'woocommerce_order_status_changed', 'order_stock_reduction_based_on_status', 20, 4 );
function order_stock_reduction_based_on_status( $order_id, $old_status, $new_status, $order ){
// Only for 'processing' and 'hold-on' order statuses change
if ( $new_status == 'processing' || $new_status == 'hold-on' ) {
// Checking if this has already been done avoiding reload
$delivery_order_id = get_post_meta($order_id, 'delivery_order_id', true);
if ( ! empty( $delivery_order_id ) ) {
return; // Exit if already processed
}
$order_data = $order->get_data();
// Send data
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://app.example.com/api/index.php?new_order");
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_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
$decoded = (array) json_decode($result);
// Output
if( isset($decoded['result']) && $decoded['result'] == 'success' && isset($decoded['order_id']) && !empty($decoded['order_id']) ){
update_post_meta( $order_id, 'delivery_order_id', esc_attr( $decoded['order_id'] ) );
}
} // <== <== <== <== Closing bracket HERE
}
它现在应该可以正常工作(我希望)。
推荐阅读
- xslt-2.0 - 无法在 XSLT 中使用 group by 对元素值进行分组
- c# - 如何更新用户的管理器?
- html - 时刻未设置为时间类型
- python - Python:将变量发送到另一个脚本而不运行
- android - 使用 RxJava 从用户列表中提取用户名
- c++ - 使用命令行参数从 txt 文件中提取并在 C++ 中运行特定的类
- php - .htaccess 代码更改以从内页中删除 php 扩展名
- firebase - 如果电子邮件存在或不存在,则始终返回 false
- python - 使用 Jaydebeapi 将 Pandas DataFrame 写入 Teradata 数据库表给出错误
- javascript - 下载大型 CSV 文件 - href 无意中截断结果