How to send cancelled order emails to customers in WooCommerce
If you’re running an online store with WooCommerce, you may occasionally have to cancel orders due to various reasons. When you cancel an order, it’s important to notify the customer about the cancellation and provide a reason for the cancellation. Unfortunately, WooCommerce by default only sends the order cancellation email to the WooCommerce admin and not the customer itself. In this tutorial, we will walk through the process of sending canceled order emails to customers in WooCommerce using a simple code snippet.
What is an order cancellation email in WooCommerce?
An order cancellation email in WooCommerce is an email sent to the WooCommerce customer when they cancel their order. This email is sent out by the admin, who has set up the settings to automatically send out an order cancellation email when a customer cancels an order in WooCommerce. The recipient of this email would be the customer, and it will include their specific email address. When this email is sent, it will contain information regarding their canceled order, such as the items that were ordered and any refunds or credits that were applied. This type of communication helps keep customers informed of their orders in a Woocommerce store.
Setting up email notifications in WooCommerce
The first step in sending canceled order emails to customers is to make sure you have set up email notifications in WooCommerce properly. Here’s how to check:
- Log in to your WordPress dashboard and navigate to the WooCommerce > Settings page.
- Click on the Emails tab.
- Scroll down to the Cancelled Order section and make sure the Enable/Disable checkbox is checked.
- Click on the Save changes button.
With this setting enabled, your customers will receive email notifications.
Writing the PHP code to send the canceled order email
Before we start with writing the code itself we want to make sure that we know how to add the custom code to your WooCommerce store.
You can add custom code to WordPress using the Code Snippets plugin:
- Install and activate the Code Snippets plugin. You can do this by going to the “Plugins” section in your WordPress dashboard, clicking on “Add New,” searching for “Code Snippets,” and then installing and activating the plugin.
- To add a new custom code snippet, click on the “Add New” button at the top of the page.
- Next, paste your custom code into the “Code” section.
- Once you have added your code, click the “Save Changes and Activate” button.
And you are done! With the Code Snippets plugin, you can easily add and manage custom code snippets on your WordPress site without having to edit your theme files directly.
Alternatively, you can use the WordPress theme. You can put the PHP code to send an email to the customer when an order is canceled in the themes functions.php file. To do this:
- Log in to your WordPress dashboard and navigate to Appearance > Theme Editor.
- Click on the functions.php file.
- Add the following code to the file:
Not let’s focus on the actual code. Essentially, this code modifies the default WooCommerce behavior (that sends the email to the WooCommerce admin) to ensure that the customer receives an email when an order is canceled or fails. By changing the recipient of the relevant email instance to the customer email and triggering the email, the customer is automatically notified of the change in order status.
add_action('woocommerce_order_status_changed', 'send_cancelled_email_notifications', 10, 4 ); function send_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 ); } }
This code uses the WooCommerce woocommerce_order_status_changed
hook to call the send_cancelled_order_email
function when an order is canceled.
TheĀ send_cancelled_email_notifications
function then checks if the new status is either “cancelled” or “failed”. If the new status matches either of these values, the function gets all instances of the WC_Emails
class (which are responsible for sending WooCommerce emails) and the email address of the customer who placed the order.
If the new status is “cancelled”, the recipient of the WC_Email_Cancelled_Order
instance (which is responsible for sending the “Order Cancelled” email) is set to the customer email, and the email is triggered with the trigger
function. If the new status is “failed”, the recipient of the WC_Email_Failed_Order
instance (which is responsible for sending the “Order Failed” email) is set to the customer email, and the email is triggered with the trigger
function.
Testing the Cancelled Order Email
Once you’ve created a custom template and customized the email content, it’s time to test the canceled order email. Here’s how:
- Create a test order in WooCommerce and mark it as canceled.
- Check the email address you specified in the Recipient(s) field to make sure the canceled order email was sent.
- Review the email content and ensure that it includes all the necessary information.
- Troubleshoot any issues that may arise.
Testing the canceled order email is an important step in ensuring that your customers receive accurate and timely information about their canceled orders.
Unlock the Full Potential of Your WooCommerce Customer Data
Conclusion
In this tutorial, we walked through the process of sending canceled order emails to customers in WooCommerce using a simple code snippet. We first set up email notifications for canceled orders and then, we wrote the PHP code to send the email when an order is canceled.
By following these steps, you can provide a better customer experience by notifying them of canceled orders and giving them the details for the order cancellation.