How to delay the sending of the WooCommerce order complete email?
To delay the sending of the WooCommerce order complete email by 10 minutes, you would need to make use of a custom code snippet in your WordPress theme’s functions.php
file or in a custom plugin. Here’s an example of how you can achieve this:
- Open your theme’s
functions.php
file or create a custom plugin. - Add the following code snippet:
/**
* Delay WooCommerce order complete email.
*
* @param int $order_id The order ID.
*/
function delay_woocommerce_order_complete_email($order_id) {
// Set the delay time in minutes
$delay_minutes = 10;
// Calculate the delay time
$delay_time = $delay_minutes * 60;
// Schedule the email to be sent after the delay
wp_schedule_single_event(time() + $delay_time, 'send_woocommerce_order_complete_email', array($order_id));
}
/**
* Send delayed WooCommerce order complete email.
*
* @param int $order_id The order ID.
*/
function send_delayed_woocommerce_order_complete_email($order_id) {
// Get the order object
$order = wc_get_order($order_id);
// Send the order complete email
$order->send_order_completed_notification();
}
// Hook the delay function to the order status transition
add_action('woocommerce_order_status_completed', 'delay_woocommerce_order_complete_email');
// Hook the send function to the scheduled event
add_action('send_woocommerce_order_complete_email', 'send_delayed_woocommerce_order_complete_email');
- Save the changes to the
functions.php
file or activate your custom plugin.
Now, whenever an order transitions to the “Completed” status in WooCommerce, the delay_woocommerce_order_complete_email
function will be triggered, which schedules the send_delayed_woocommerce_order_complete_email
function to run after the specified delay (10 minutes). Once the delay time elapses, the order complete email will be sent to the customer.
Please note that this code assumes you’re using the default WooCommerce order complete email functionality. If you have a custom email setup or are using a third-party plugin for sending order complete emails, you may need to adjust the code accordingly.