My orders are automatically sent to the external delivery service. But when the status of the order changes, it is again sent to the external delivery service. How to fix it?
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'] ) );
}
}
Aucun commentaire:
Enregistrer un commentaire