How to Send Email on Status Change In WooCommerce

WooCommerce Send Email on Status ChangeAre you looking for a way to send a custom email on order status change? WooCommerce is packed with many features that allow you to sell anything online. WooCommerce includes core order statuses that define the workflow of your store.

After each status, the shopper is notified via email. For example, when the order goes from “Processing” to “Completed”, the Completed email is sent to the customer.

However, some of them may not fit the workflow of your online store. This is why many store owners use custom order statuses.

It is worth mentioning that if you want to target a transition, for example from “Processing” to “Custom Status”, no email will trigger.

This means that we have to send a custom email.

WooCommerce Send Email on Status Change

In this post, we will show you how to trigger an email for a custom order status. It is important to note that we will be using custom code to achieve this.

Therefore, we recommend creating a child theme before you proceed. This will ensure that your changes are not lost during an update.

Let us look at how you can implement this solution in your store.

Steps to Send Email on Status Change

If it is your first time adding custom code, please take a full backup of your site. This will help you revert to the previous version if you encounter any challenges.

We will be creating a custom email for the “Refused” status:custom status

Here are the steps you need to follow:

  1. Log into your WordPress site and access the Dashboard as the admin user.
  2. From the Dashboard menu, click on Appearance Menu > Theme Editor Menu. When the Theme Editor page is opened, look for the theme functions file where we will add the function that will send a custom email on status change.
  3. Add the following code to the php file:
[php]

/**
 * @snippet       Send Formatted Email @ WooCommerce Custom Order Status
  */

// Targets custom order status "refused"

// Uses ‘woocommerce_order_status_’ hook

add_action( ‘woocommerce_order_status_refused’, ‘njengah_status_custom_notification’, 20, 2 );

function njengah_status_custom_notification( $order_id, $order ) {
    $heading = ‘Order Refused’;

    $subject = ‘Order Refused’;

    // Get WooCommerce email objects

    $mailer = WC()->mailer()->get_emails();

    $mailer[‘WC_Email_Customer_Completed_Order’]->heading = $heading;

    $mailer[‘WC_Email_Customer_Completed_Order’]->settings[‘heading’] = $heading;

    $mailer[‘WC_Email_Customer_Completed_Order’]->subject = $subject;

    $mailer[‘WC_Email_Customer_Completed_Order’]->settings[‘subject’] = $subject;

    // Send the email with custom heading & subject

    $mailer[‘WC_Email_Customer_Completed_Order’]->trigger( $order_id );

    // You have to use the email ID chosen above and also that $order->get_status() == "refused"

}

[/php]

After adding this code, custom emails will be sent on status change. Remember to use your custom order status name in the code.

Conclusion

By now, you should be able to send custom emails on status change. You can add different sections to the email depending on your needs. If you need further help you can consider getting in touch with me.

Similar Articles