You can use meta to create a one time action
something like this
$order_id = $order->get_id();
if (get_post_meta($order_id, 'is_mail_sent', true)) return;
update_post_meta($order_id, 'is_mail_sent', true);
Add this right after the opening of the if
block
This code will first check if the order meta value of is_mail_sent
is true, if true it will return (exit) from the funcion.
If it was not true, first time, then it will continue to the rest of the code and also set the is_mail_sent
to true to prevent future mail sending
You could also do this
$order_id = $order->get_id();
if($now>$last_time && !get_post_meta($order_id, 'is_mail_sent', true)) {
update_post_meta($order_id, 'is_mail_sent', true);
$order_number = $order->get_order_number();
$to = '[email protected]';
$subject="Failed order ";
$body = '#'.$order_number. ' order number failed;
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $body, $headers );
}
This will perform the same check but will only wrap the mail part of the code