If you use custom WooCommerce order statuses, sending emails is quite complex because you need to code a full custom email PHP class.
At least on paper!
In this workaround, we will actually “use” one of the existing email classes (note: it must be active in your WooCommerce email settings in order to work), change the subject and heading, and trigger it – like if it were a custom order status email.
Enjoy!
PHP Snippet: Send WooCommerce Email @ Custom Order Status
/**
* @snippet Send Formatted Email @ WooCommerce Custom Order Status
* @how-to businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @compatible WooCommerce 8
* @community https://businessbloomer.com/club/
*/
// Targets custom order status "refused"
// Uses "'woocommerce_order_status_' . $status" hook
add_action( 'woocommerce_order_status_refused', 'bbloomer_status_custom_notification', 20, 2 );
function bbloomer_status_custom_notification( $order_id, $order ) {
$subject = 'Order Refused';
$heading = 'Sorry!';
// Get WooCommerce email objects
$mailer = WC()->mailer()->get_emails();
// Use one of the active emails e.g. "Customer_Completed_Order"
// Wont work if you choose an object that is not active
// Assign heading & subject to chosen object
$mailer['WC_Email_Customer_Completed_Order']->settings['heading'] = $heading;
$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 );
// To add email content use https://businessbloomer.com/woocommerce-add-extra-content-order-email/
// You have to use the email ID chosen above and check that $order->get_status() == "refused"
}
Here’s a full list of default WooCommerce email classes you can use in the snippet above:
- WC_Email_New_Order: Sent to the store admin when a new order is created.
- WC_Email_Cancelled_Order: Sent to the store admin when an order is cancelled.
- WC_Email_Failed_Order: Sent to the store admin when an order fails.
- WC_Email_Customer_On_Hold_Order: Sent to the customer when an order is placed on hold.
- WC_Email_Customer_Processing_Order: Sent to the customer after payment and contains the order details.
- WC_Email_Customer_Completed_Order: Sent to the customer when the order is marked as completed.
- WC_Email_Customer_Refunded_Order: Sent to the customer when an order is refunded.
- WC_Email_Customer_Invoice: Sent to the customer with order details and payment link.
- WC_Email_Customer_Note: Sent to the customer when a customer note is added to their order.
- WC_Email_Customer_Reset_Password: Sent to the customer when they request a password reset.
- WC_Email_Customer_New_Account: Sent to the customer when a new account is created.
These classes are located in the includes/emails
directory of the WooCommerce plugin. They can be extended or customized by creating new classes that extend these base classes if you need to modify the default email behavior.
Hi Rodolfo,
Just wanted to ask what is the best way to apply this PHP code if my theme is deactivated because I am using Oxygen Builder.?
Many Thanks,
oli
Use the Code Snippets plugin
Hi there,
I have a status using this plugin named “reminder”. Now I have also created email template for that reminder as well.
Now I want to add this email as trigger in processing order email. I mean to say that whenever I change status from reminder to processing. The processing email should be sent to customer. It is not sending right now.
How can I achieve this?
Thanks
Hi Mujahid thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hi, how can I change the snippet to send emails for 2 different custom order statuses? Thanks
Hi Radek, thanks so much for your comment! Yes, this is definitely possible, but Iβm afraid itβs custom work. If youβd like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hi, We used a plugin to add custom “shipped” status to order and used your snippet to send customer email with customized heading and subject. It works perfectly for a while, and we only noticed a problem since this morning:
the email can still be send out after changing order status to “shipped”(that means it still triggers the action),
but the custom subject and headings are gone, our customers are getting “Order completed email” including heading and subject.
Any updated from WordPress or Woocommerce that caused the issue? what other way to change subject or heading of the email?
Thanks a lot
Hi Damon, that’s weird. To troubleshoot, disable all plugins but WooCommerce and also switch temporarily to “Twentytwenty” theme (load the snippet there in functions.php) as explained here: https://www.businessbloomer.com/lesson/trwm4l01/
Once you do that, does it work? If yes, you have a problem with your current theme or one of the plugins.
Hope this helps!
Hi Rodolfo ,
I have put a comment here before but it doesn’t seem to have been approved…
Everything works wonderfully well but it will only send the email if it specifies the WC_Email_Customer_Completed_Order email. If you try and specify one that is unused such as ‘On Hold’ and use the correct hooks, it fails and won’t finish the cycle. Change it back to the completed order email and works again without issue.
Any thoughts why this may be? The email is turned on, not used for anything else and I have re-created this on two different sites? Thank you
Sorry I have no idea. Sure the On Hold is enabled?
Hi Rodolfo, it’s not working on my website…
Sure “shipped” is the correct email ID?
I’m sorry but what do you mean by email ID? Completely new to PHP π
Hi it’s now working! There’s conflict with my other plugin.
The only problem is it doesn’t send email to gmail or yahoo. I can only receive notification from the custom status if sending to my own email domain.
Not sure Jackie, but well done for figuring that out
Hi it’s not working for me π I’ve changed “refused” to “shipped”.
Hey! How can I get a webhook for a custom order status and By replacing the webhook of refuse from the above code, will It work as same
Hi Mudit, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hi,
I’m looking to create 2 Order Statuses for Preorders.
Preorder On-hold (Before Payment) –> Copy Email template from On-Hold
Preorder Processing (Confirmed after Payment) –> Copy Email template from Processing
There will be a Preorder Stock status as well, how can I create that using functions.php too?
Preorder On-hold will be triggered when Customer buys a Preorder product(Preorder stock status), and pends for BACS payment like the rest of the normal instock items.
Preorder Processing will be manually triggered by myself when Customer made payment.
I actually see that its possible to create using your above coding but got confused…… I supposed the Order status should be separately created? (Your Order Status here is Refused)
Thanks.
Yes
How to get custom mails as admin for order payment finished via PayPal? When you don’t use the PayPal notifications because it’s the secondary PayPal mail.
Hi Andre, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hey, where am I supposed to include the custom status slug/name? I see you commented that I should include “$order->get_status() == “refused” ” but where? Cheers!
Hey Joao! Just change “refused” with your custom order slug here:
Hi, thank you for this information. I’m interested in the following. After a payment is made in Woocommerce we place the order into ‘Processing’ than 5 business days later we change the status to ‘Awaiting Shipping’, using the following plugin to add a custom email then after the item ships we mark the order status as ‘Completed’.
We would like to automate the part that changes the status to ‘Awaiting Shipping’. Is that possible? So after 5 business days, the order status updates automatically to Awaiting Shipping.
Hi there, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!
Hey, will this allow my website to send “delivery on route” emails? Also are these emails automatic after installing your code ?
Hello Jacqui, yes & yes π
Where do I add content from https://businessbloomer.com/woocommerce-add-extra-content-order-email/
Within same function ?
Pl advice with example. thanks.
Hey Kumar, it’s explained in the snippet comments. Unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R