In some online stores, customers are given an option to cancel their orders by adding a cancel button in their My Account page. Sometimes, the store might have to cancel your order too, maybe because of payment issues or other reasons. Either way, it’s important to let customers know when their orders are cancelled.
In this post, we will be sending cancelled or failed order emails to customers. This customization makes sure that your customers are always in the know!
Solution: Send Email to Customer on Cancelled Order in WooCommerce
The below code listens for changes in the order status, specifically for “cancelled” or “failed” orders, and sends the cancelled or failed email notifications to customers accordingly.
add_action('woocommerce_order_status_changed', 'ts_cancelled_email_notifications', 10, 4 ); function ts_cancelled_email_notifications( $order_id, $old_status, $new_status, $order ){ if ( $new_status == 'cancelled' || $new_status == 'failed' ){ $wc_emails = WC()->mailer()->get_emails(); // Get all WC_emails objects instances $customer_email = $order->get_billing_email(); // Get the customer email } if ( $new_status == 'cancelled' ) { // change the recipient of the instance $wc_emails['WC_Email_Cancelled_Order']->recipient = $customer_email; // Sending the email from this instance $wc_emails['WC_Email_Cancelled_Order']->trigger( $order_id ); } elseif ( $new_status == 'failed' ) { // change the recipient of the instance $wc_emails['WC_Email_Failed_Order']->recipient = $customer_email; // Sending the email from this instance $wc_emails['WC_Email_Failed_Order']->trigger( $order_id ); } }
Output
When an order is changed to ‘cancelled’ or ‘failed’ statuses, the corresponding email template for cancelled or failed orders is triggered. Subsequently, the respective email notification will be sent to the customer.
Similarly, you can override WooCommerce default behavior of emails originally intended for the admin to be also sent to customer emails. For instance , checkout this post that will send email for pending payment status to customers in WooCommerce.