如何在元数据中保存送货服务的订单编号?
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;
-
{"result":"success","order_id":100888,"order_number":10}
需要为此新订单保存"order_id":100888
。
改为尝试以下操作
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'] ) );
}
}
代码进入函数.php活动子主题(或活动主题(的文件。它应该有效。