Create an email for custom order status in WooCommerce

In past posts, we learned how to add a custom order status, how to programmatically change a status, and how to hook into WooCommerce status changes.

In this post, we’ll learn how to create a custom WooCommerce email notification for a custom order status.

Instead of creating/registering a whole new custom email template, we are going to use a quick and dirty method.

Let’s get started!

Step 1: Order status change hook

For this example, I am going to create an email for a custom order status called “Shipped”.

So, the first step is to set up a basic function to run when an order status changes to “Shipped”.

add_action( 'woocommerce_order_status_shipped', 'shipped_custom_notification', 20, 2 );

function shipped_custom_notification( $order_id, $order ) {

    // do stuff here

}

Step 2: Using WooCommerce mailer() method

Now we’ll setup a new email using the builtin WooCommerce email class.

Let’s take a look at the code, and then break it down after:

add_action( 'woocommerce_order_status_shipped', 'shipped_custom_notification', 20, 2 );

function shipped_custom_notification( $order_id, $order ) {

    // 1. Create a new instance to use from WooCommerce email class
  	$shipped_mailer = WC()->mailer();  

    // 2. setup email content -- heading and body 
    $heading = 'Your order was shipped 📦';

    $body = "
        <p>Hi $order->billing_first_name, </p>
        <p>Your order (#$order_id) was shipped.  It should arrive soon!</p>
    ";

    // 3. Wraps content in the default Woocommerce email template.   
    $message = $shipped_mailer->wrap_message( $heading, $body );

    // 4. send email
 	$email = $order->billing_email;
    $subject = 'Your order was shipped!';
    $shipped_mailer->send( $email, $subject, $message );

}

Now, let’s break down each step:

  1. WC()->mailer(); creates a new email instance for us to use, and assigns it to a variable shipped_mailer. The WC()function returns an instance of WooCommerce (you could also use global $woocommerce).
  2. This is just the content that shows in the email. You can add custom HTML content, and include any data you want from the $order object such as customer details, products, custom fields etc.
  3. The wrap_message() method formats the email content nicely using the default WooCommerce email template.
  4. The last step simply sends the email using builtin send() method. You must specify the $email to send to, $subject of email, and the $message. You can also specify email headers and attachments if you need to.

Result:

Whenever an order status is changed to “Shipped”, the customer will get this email:

woocommerce custom order status email

Summary

We looked at a quick and dirty way to send emails for custom order statuses in WooCommerce by using the builtin email class. You can make the email as fancy as you need to by simply customizing the $message_content.

There are probably many other ways to send a a custom order status email. If you want to create/register a full-on email template, check out this tutorial by Ibenic.

Hope this helps!

😎

Get Weekly WordPress tips

Was this helpful? Every week, I post a useful tip about WordPress and general web development. Subscribe below to get updates by email. (It's free 🙂)

Leave a Comment