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.

woocommerce cancelled order email to customer

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:

  1. Log in to your WordPress dashboard and navigate to the WooCommerce > Settings page.
  2. Click on the Emails tab.
  3. Scroll down to the Cancelled Order section and make sure the Enable/Disable checkbox is checked.
  4. Click on the Save changes button.

With this setting enabled, your customers will receive email notifications.

WooCommerce allow customer to cancel order

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:

  1. 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.
  2. To add a new custom code snippet, click on the “Add New” button at the top of the page.
  3. Next, paste your custom code into the “Code” section.
  4. 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:

  1. Log in to your WordPress dashboard and navigate to Appearance > Theme Editor.
  2. Click on the functions.php file.
  3. 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.

 

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:

  1. Create a test order in WooCommerce and mark it as canceled.
  2. Check the email address you specified in the Recipient(s) field to make sure the canceled order email was sent.
  3. Review the email content and ensure that it includes all the necessary information.
  4. 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.