本文介绍了将收到的数据从Woocommerce 3中的外部交付服务保存到订单中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在元数据中保存来自送货服务的订单ID?
How to save the order id from delivery service in metadata?
add_action('woocommerce_thankyou', 'send_order_to_delivery');
function send_order_to_delivery( $order_id ){
// Send data
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://app.axample.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);
$json = '$result';
$delivery_order_id = json_decode($json)->order_id;
if ( ! empty( $_POST['delivery_order_id'] ) ) {
update_post_meta( $order_id, 'delivery_order_id', sanitize_text_field( $_POST['delivery_order_id'] ) );
}
}
当我将订单发送到外部递送服务时,我得到答案 echo $ result;
-
When I send the order to an external delivery service, I get the answer echo $result;
-
{"result":"success","order_id":100888,"order_number":10}
需要保存 order_id:100888
用于此新订单。
Need save "order_id":100888
for this new order.
推荐答案
请尝试以下操作
add_action('woocommerce_thankyou', 'send_order_to_delivery');
function send_order_to_delivery( $order_id ){
// Send data
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://app.axample.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);
// Test 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'] ) );
}
}
代码会出现在您活跃孩子的function.php文件中主题(或活动主题)。应该可以。
Code goes in function.php file of your active child theme (or active theme). It should works.
这篇关于将收到的数据从Woocommerce 3中的外部交付服务保存到订单中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!