Sometimes the function woocommerce_thankyou is not called, but sometimes works fine.
Our code is:
add_action(‘woocommerce_thankyou’, ‘send_order_information_for_delivery’, 999, 1);
function send_order_information_for_delivery($order_id)
{
$order = wc_get_order($order_id);
$order_items = $order->get_items();
// … …
}
Any idea why sometimes doesn’t work?
The main objective of this method is to obtain the information of the purchase order and its items and to send them to another database through an API.
The strange thing is that in some orders the method is not called.
Instead, you could try to use the following hooked function, that will work for defined order statuses only once, after the delivery data is processed.
add_action( 'woocommerce_order_status_changed', 'delivery_information_process', 20, 4 );
function delivery_information_process( $order_id, $old_status, $new_status, $order ){
// Define the order statuses to check
$statuses_to_check = array( 'on-hold', 'processing' );
// Only "On hold" order status and "Free Shipping" Shipping Method
if ( $order->get_meta( '_delivery_check', true ) && in_array( $new_status, $statuses_to_check ) )
{
// Getting all WC_emails objects
foreach($order->get_items() as $item_id => $item ){
$product = $item->get_product();
$sku = $product->get_sku();
}
## ==> Process delivery data step
// Once delivery information is send or processed ==> update '_delivery_check' to avoid repetitions
$order->update_meta_data( '_delivery_check', true );
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.