防止在 Woocommerce thankyou hook 中多次处理数据
Prevent processing data multiple times in Woocommerce thankyou hook
我有一个代码可以将订单数据发送到外部交付服务,在那里创建一个新订单。我注意到,如果您重新加载 "Thank you" 页面,代码将再次运行并发送相同的信息。结果:在支持服务中创建了多个相同的订单。我应该在代码中添加什么才不会发生这种情况?
add_action('woocommerce_thankyou', 'send_order_to_delivery');
function send_order_to_delivery( $order_id ){
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
$order_data = $order->get_data();
$order_id = $order_data['id'];
// 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'] ) );
}
}
尝试以下方法,我们检查 'delivery_order_id'
meta_key
的订单元数据中是否已经存在某些值,避免执行此任务两次或更多次:
add_action('woocommerce_thankyou', 'send_order_to_delivery');
function send_order_to_delivery( $order_id ){
// Checking if this has already been done avoiding reload
if( get_post_meta( $order_id, 'delivery_order_id', true ) )
return; // Exit if already processed
// Get an instance of the WC_Order object
$order_data = $order->get_data();
$order_id = $order_data['id'];
// 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'] ) );
}
}
代码进入活动子主题(或活动主题)的 function.php 文件。它应该最终解决你的问题。
我有一个代码可以将订单数据发送到外部交付服务,在那里创建一个新订单。我注意到,如果您重新加载 "Thank you" 页面,代码将再次运行并发送相同的信息。结果:在支持服务中创建了多个相同的订单。我应该在代码中添加什么才不会发生这种情况?
add_action('woocommerce_thankyou', 'send_order_to_delivery');
function send_order_to_delivery( $order_id ){
// Get an instance of the WC_Order object
$order = wc_get_order( $order_id );
$order_data = $order->get_data();
$order_id = $order_data['id'];
// 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'] ) );
}
}
尝试以下方法,我们检查 'delivery_order_id'
meta_key
的订单元数据中是否已经存在某些值,避免执行此任务两次或更多次:
add_action('woocommerce_thankyou', 'send_order_to_delivery');
function send_order_to_delivery( $order_id ){
// Checking if this has already been done avoiding reload
if( get_post_meta( $order_id, 'delivery_order_id', true ) )
return; // Exit if already processed
// Get an instance of the WC_Order object
$order_data = $order->get_data();
$order_id = $order_data['id'];
// 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'] ) );
}
}
代码进入活动子主题(或活动主题)的 function.php 文件。它应该最终解决你的问题。