Site icon MIND Development & Design, LLC

Troubleshooting Files: How to Send Specific Order Process Emails in WooCommerce

faded laptop in background with html code highlighted on the screen

We had a helpdesk ticket come through from someone with a WooCommerce site who wanted to send different messages out in customer emails depending on which shipping method the customer used. There are a few ways to do that with WooCommerce. But, allowing the site owner or developers working on the updates to preview those email messages is not as straight-forward since the old “Order Actions” dropdown was simplified a few years ago. If you need to send specific order process emails in WooCommerce, like “Order Processing” or “Order Complete”, rather than the generic customer invoice, here is how to do it:

1. Figure Out Where To Start

Years ago, in earlier versions of WooCommerce, there were a few more options available in this dropdown:

But, those options were removed back in 2017.

Digging into WooCommerce’s source code, we find a few hooks we can use to put these options back in or add any other custom order actions. Getting the custom order actions into the “wc_order_action” dropdown is simple enough. After all, there’s a hook for that – woocommerce_order_actions

2. Add New Options to the Order Actions Dropdown

We can hook in here and add our custom actions. We’re going to add the Order Processing email, specifically, and denote it with an asterisk. Usually, this email is only sent when the order is actually “processing” in the system and can’t be resent once an order completes, but this allows us to resend it as needed. It should look something like this:

add_action( 'woocommerce_order_actions', 'mind_woo_order_actions' );

function mind_woo_order_actions( $actions ) {
    $actions['resend_order_processing_email'] =  '* Resend "Order Processing" Email';
    return $actions;
}

3. Define the Function to Run for the New Custom Action

Next, we need to define what happens when someone selects our custom action. WooCommerce automatically generates a hook based on the way you defined the custom order action in the last function. So, make sure you remember your action’s name, which is the key in the $actions array, and hook in with the “wc_order_action_” prefix. Where our action was “resend_order_processing_email”, we can hook into “woocommerce_order_action_resend_order_processing_email” with our custom function., like this:

add_action( 'woocommerce_order_action_resend_order_processing_email', 'mind_woo_resend_order_processing_email' );

function mind_woo_resend_order_processing_email( $order ) {
 // do stuff
}

4. Find the WooCommerce Email to Trigger

Now, we can use the built-in email triggers for WooCommerce to send the appropriate emails we want. Reference the WC Email class to make sure you’re using the right one.

add_action( 'woocommerce_order_action_resend_order_processing_email', 'mind_woo_resend_order_processing_email' );

function mind_woo_resend_order_processing_email( $order ) {
       $order_id = $order->get_id();
       $allmails = WC()->mailer()->emails;
       $email = $allmails['WC_Email_Customer_Processing_Order'];
       $email->trigger( $order_id );
}

You Can Add an Order Note Too

Let’s add an order note too, showing that it was resent:

$order->add_order_note( '"Order Processing" Email Resent' );

The Complete Code for Sending Specific Order Process Emails in WooCommerce

Putting everything together, it should look a little something like this:

add_action( 'woocommerce_order_actions', 'mind_woo_order_actions' );
function mind_woo_order_actions( $actions ) {
    $actions['resend_order_processing_email'] =  '* Resend "Order Processing" Email';
    return $actions;
}


add_action( 'woocommerce_order_action_resend_order_processing_email', 'mind_woo_resend_order_processing_email' );
function mind_woo_resend_order_processing_email( $order ) {
        $order_id = $order->get_id();
        $allmails = WC()->mailer()->emails;
        $email = $allmails['WC_Email_Customer_Processing_Order'];
        $email->trigger( $order_id );
        $order->add_order_note( '"Order Processing" Email Resent' );
}

Enjoy your new order action to resend specific order process emails in WooCommerce!

Exit mobile version