Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Send Email For Custom Order Status Change in Woocommerce?

To better manage the order processing workflow, you would have implemented a custom order status either via a plugin or using custom coding. As a next step, it is also essential to keep customers informed about the progress of their orders via email notifications. Though this built-in feature follows a systematic approach to sending emails only for certain order statuses we can customize it to send emails for custom order statuses as well.

Solution: Send Email For Custom Order Status Change in Woocommerce

The below code snippet will create a custom order status ‘Awaiting Delivery’ and will send email notifcations when the order transitions to this custom order status.

// Register a custom post status 'awaiting-delivery' for Orders
add_action( 'init', 'ts_register_custom_post_status', 20 );
function ts_register_custom_post_status() {
    register_post_status( 'wc-awaiting-delivery', array(
        'label'                     => _x( 'Awaiting delivery', 'Order status', 'woocommerce' ),
        'public'                    => true,
        'exclude_from_search'       => false,
        'show_in_admin_all_list'    => true,
        'show_in_admin_status_list' => true,
        'label_count'               => _n_noop( 'Awaiting delivery <span class="count">(%s)</span>', 'Awaiting delivery <span class="count">(%s)</span>', 'woocommerce' )
    ) );
}

// Adding custom status 'awaiting-delivery' to order edit pages dropdown
add_filter( 'wc_order_statuses', 'ts_custom_wc_order_statuses', 20, 1 );
function ts_custom_wc_order_statuses( $order_statuses ) {
    $order_statuses['wc-awaiting-delivery'] = _x( 'Awaiting delivery', 'Order status', 'woocommerce' );
    return $order_statuses;
}

// Adding action for 'awaiting-delivery'
add_filter( 'woocommerce_email_actions', 'ts_custom_email_actions', 20, 1 );
function ts_custom_email_actions( $action ) {
    $actions[] = 'woocommerce_order_status_wc-awaiting-delivery';
    return $actions;
}

add_action( 'woocommerce_order_status_wc-awaiting-delivery', array( WC(), 'send_transactional_email' ), 10, 1 );

// Sending an email notification when order gets 'awaiting-delivery' status
add_action('woocommerce_order_status_awaiting-delivery', 'ts_backorder_status_custom_notification', 20, 2);
function ts_backorder_status_custom_notification( $order_id, $order ) {
    // Here below your settings
    $heading   = __('Your Awaiting delivery order','woocommerce');
    $subject   = '[{site_title}] Awaiting delivery order ({order_number}) - {order_date}';

    // Getting all WC_emails objects
    $mailer = WC()->mailer()->get_emails();

    // Customizing Heading and subject in the WC_email processing Order object
    $mailer['WC_Email_Customer_Processing_Order']->heading = $heading;
    $mailer['WC_Email_Customer_Processing_Order']->subject = $subject;

    // Sending the customized email
    $mailer['WC_Email_Customer_Processing_Order']->trigger( $order_id );
}
add_filter( 'gettext', 'ts_translate_woocommerce_strings_emails', 10, 3 );
  
function ts_translate_woocommerce_strings_emails( $translated, $untranslated, $domain ) {
   if ( 'woocommerce' === $domain ) {   
      $translated = str_ireplace( 'Hi %s,', '', $untranslated ); // HIDE
      $translated = str_ireplace( 'Just to let you know &mdash; we\'ve received your order #%s, and it is now being processed:', 'Your payment was successful and we are now getting ready for shipping. Expect another email from us with tracking details soon!', $untranslated ); // EDIT
   }
   return $translated;
}

Output

Whenever the order gets changed to the custom status ‘Awaiting delivery’ status, the default email of the processing order email is triggered and email notification will be sent to the customer. The code also hides the default message of the processing order email and will display a customized content that suits the custom order status.

Send Email For Custom Order Status Change in Woocommerce

If you wish to add colors to the order status you can also refer to this post that will change the default WooCommerce Order status colors. Additionally, you may feel to send a customized email template for custom order statuses. In such situations, you checkout our post that provides a step by step guide to customize WooCommerce Order Emails.



Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
0 Comments
Newest
Oldest
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x