This tut has been updated to work with WooCommerce 3.0+. Read how can you add and send custom WooCommerce email for WooCommerce version 3.0 and higher.
Emails are pretty important when you have customers purchasing products from your online store. Customers buying your products (even us, when we make an online purchase), be it physical or virtual, expect to receive some sort of emails about the confirmation of payment, the processing of order and so on. If your store fails to do so, you’re probably doing it wrong and making your business suffer. Because, the customer has just sent you his money and they’ve not yet heard back from you, they’ve not received a payment confirmation. Get the problem?
If you’re selling with WordPress & WooCommerce, things are quite easy for you as a store owner/manager. WooCommerce, by default, sends out some emails to let your customers know that their order has been placed, processed and/or is complete. However, sometimes it may happen that based on the products you’re selling, you may need to send out custom emails to the customers once they’ve placed an order on your site.
Practical Example
For instance, let’s say, you have a WordPress & WooCommerce powered online store that allows customers to purchase a course on your website and in-turn grants them access to your online training program.
In such a case, you may want to send out a Welcome Email to the customers informing them about the purchase and sharing access details for the online training program. In order to be able to do so, we’ll need to add a custom WooCommerce email that triggers every time a new order is placed on the site. WooCommerce 2.0 introduced a whole bunch of new features, including the ability to add a new class-based implementation for emails. In this article, we’ll use the WooCommerce email class implementation to add a custom email which is triggered on every new order once the payment has been received.
Side Note
The example cited above may also require custom implementation that allows interaction between WooCommerce and (possibly the API to connect to) your online training program to allow adding new user accounts when a customer makes a purchase at your online store. This article does not cover the integration to a third-party service part, and is strictly intended to guide you on how can a custom email be added and sent through your WooCommerce store.
Implementation
Duh.. Enough Talk! Let’s Implement That
Implementation of custom email is divided into three phases:
- Extending
WC_Email
class to define the email header, subject, email template for content, etc. - Adding our custom email class to the default WooCommerce email classes using
woocommerce_email_classes
filter - Creating our own email template to be used to generate email content for our custom email.
Let’s get started.
First things first!
It is highly recommended that any customization be kept separate from the theme. A plugin like WP Designer could come in handy if you want to add such a customization. This example requires you to FTP into your website and create custom directories and files inside wp-content > uploads
directory. Following is a list of directories and files that need to be created:
- Create directory
crwc-custom-emails
insidewp-content > uploads
. - Create file
crwc-email-functions.php
in the root ofcrwc-custom-emails
directory (crwc-custom-emails > crwc-email-functions.php
). - Create file
class-crwc-welcome-email.php
in the root ofcrwc-custom-emails
directory (crwc-custom-emails > class-crwc-welcome-email.php
). - Create a sub-directory
emails
insidecrwc-custom-emails
directory. - Create a new file
crwc-welcome-email.php
inside theemails
directory (crwc-custom-emails > emails > crwc-welcome-email.php
). - You can also choose to create a sub-directory
plain
insideemails
directory and then createcrwc-welcome-email.php
file insideplain
directory (crwc-custom-emails > emails > plain > crwc-welcome-email.php
). This will be used in case emails use plain content-type.
Building custom email class
The custom email class that we create would actually define the email trigger plus the email content. We can extend the default WC_Email
class and hence make use of all of the parent email class methods & members that are available. This makes life really easy 😉
Add the following code to class-crwc-welcome-email.php
file.
<?php if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly /** * Welcome Email class used to send out welcome emails to customers purchasing a course * * @extends \WC_Email */ class CRWC_Welcome_Email extends WC_Email { /** * Set email defaults */ public function __construct() { // Unique ID for custom email $this->id = 'crwc_welcome_email'; // Is a customer email $this->customer_email = true; // Title field in WooCommerce Email settings $this->title = __( 'Welcome Email', 'woocommerce' ); // Description field in WooCommerce email settings $this->description = __( 'Welcome email is sent when an online training program account is created for the customer after the purchase of the online course.', 'woocommerce' ); // Default heading and subject lines in WooCommerce email settings $this->subject = apply_filters( 'crwc_welcome_email_default_subject', __( 'XYZ Online Training Program', 'woocommerce' ) ); $this->heading = apply_filters( 'crwc_welcome_email_default_heading', __( 'Welcome to Online Training Program', 'woocommerce' ) ); // these define the locations of the templates that this email should use, we'll just use the new order template since this email is similar $upload_dir = wp_upload_dir(); $this->template_base = $upload_dir['basedir'] . '/crwc-custom-emails/'; // Fix the template base lookup for use on admin screen template path display $this->template_html = 'emails/crwc-welcome-email.php'; $this->template_plain = 'emails/plain/crwc-welcome-email.php'; // Trigger email when payment is complete add_action( 'woocommerce_payment_complete', array( $this, 'trigger' ) ); add_action( 'woocommerce_order_status_on-hold_to_processing', array( $this, 'trigger' ) ); add_action( 'woocommerce_order_status_on-hold_to_completed', array( $this, 'trigger' ) ); add_action( 'woocommerce_order_status_failed_to_processing', array( $this, 'trigger' ) ); add_action( 'woocommerce_order_status_failed_to_completed', array( $this, 'trigger' ) ); add_action( 'woocommerce_order_status_pending_to_processing', array( $this, 'trigger' ) ); // Call parent constructor to load any other defaults not explicity defined here parent::__construct(); } /** * Prepares email content and triggers the email * * @param int $order_id */ public function trigger( $order_id ) { // Bail if no order ID is present if ( ! $order_id ) return; // Send welcome email only once and not on every order status change if ( ! get_post_meta( $order_id, '_crwc_welcome_email_sent', true ) ) { // setup order object $this->object = new WC_Order( $order_id ); // get order items as array $order_items = $this->object->get_items(); //* Maybe include an additional check to make sure that the online training program account was created /* Uncomment and add your own conditional check $online_training_account_created = get_post_meta( $this->object->id, '_crwc_user_account_created', 1 ); if ( ! empty( $online_training_account_created ) && false === $online_training_account_created ) { return; } */ /* Proceed with sending email */ $this->recipient = $this->object->billing_email; // replace variables in the subject/headings $this->find[] = '{order_date}'; $this->replace[] = date_i18n( woocommerce_date_format(), strtotime( $this->object->order_date ) ); $this->find[] = '{order_number}'; $this->replace[] = $this->object->get_order_number(); if ( ! $this->is_enabled() || ! $this->get_recipient() ) { return; } // All well, send the email $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); // add order note about the same $this->object->add_order_note( sprintf( __( '%s email sent to the customer.', 'woocommerce' ), $this->title ) ); // Set order meta to indicate that the welcome email was sent update_post_meta( $this->object->id, '_crwc_welcome_email_sent', 1 ); } } /** * get_content_html function. * * @return string */ public function get_content_html() { return wc_get_template_html( $this->template_html, array( 'order' => $this->object, 'email_heading' => $this->email_heading( $this->course_info['program'] ), 'sent_to_admin' => false, 'plain_text' => false, 'email' => $this ) ); } /** * get_content_plain function. * * @return string */ public function get_content_plain() { return wc_get_template_html( $this->template_plain, array( 'order' => $this->object, 'email_heading' => $this->email_heading( $this->course_info['program'] ), 'sent_to_admin' => false, 'plain_text' => true, 'email' => $this ) ); } /** * Initialize settings form fields */ public function init_form_fields() { $this->form_fields = array( 'enabled' => array( 'title' => __( 'Enable/Disable', 'woocommerce' ), 'type' => 'checkbox', 'label' => 'Enable this email notification', 'default' => 'yes' ), 'subject' => array( 'title' => __( 'Subject', 'woocommerce' ), 'type' => 'text', 'description' => sprintf( 'This controls the email subject line. Leave blank to use the default subject: <code>%s</code>.', $this->subject ), 'placeholder' => '', 'default' => '' ), 'heading' => array( 'title' => __( 'Email Heading', 'woocommerce' ), 'type' => 'text', 'description' => sprintf( __( 'This controls the main heading contained within the email notification. Leave blank to use the default heading: <code>%s</code>.' ), $this->heading ), 'placeholder' => '', 'default' => '' ), 'email_type' => array( 'title' => __( 'Email type', 'woocommerce' ), 'type' => 'select', 'description' => __( 'Choose which format of email to send.', 'woocommerce' ), 'default' => 'html', 'class' => 'email_type wc-enhanced-select', 'options' => array( 'plain' => __( 'Plain text', 'woocommerce' ), 'html' => __( 'HTML', 'woocommerce' ), 'multipart' => __( 'Multipart', 'woocommerce' ), ) ) ); } }
Viewing custom email settings
The code mentioned above will help us to create custom class with all settings and triggers. However, to be able to see the custom email settings that we created along with the default emails on WooCommerce Emails screen under WooCommerce > Settings > Emails
, we’ll need to add this class to the default email classes in WooCommerce. In order to do that, we’ll make use of the woocommerce_email_classes
filter to include custom email class to the default classes.
Add the following code to crwc-email-functions.php
file (inside crwc-custom-emails
directory).
<?php add_filter( 'woocommerce_email_classes', 'crwc_custom_woocommerce_emails' ); function crwc_custom_woocommerce_emails( $email_classes ) { //* Custom welcome email to customer when purchasing online training program $upload_dir = wp_upload_dir(); require_once( $upload_dir['basedir'] . '/crwc-custom-emails/class-crwc-welcome-email.php' ); $email_classes['CRWC_Welcome_Email'] = new CRWC_Welcome_Email(); // add to the list of email classes that WooCommerce loads return $email_classes; }
Now, when you navigate to WooCommerce > Settings > Emails
, you should be able to see the custom email Welcome Email that we added to the list of emails on that page. If you click on the Configure button, you will find settings screen that allows you to enable/disable the email, edit/update the email subject and heading and choose the email content type since we added these settings in the custom class we created above.
Creating custom email template
Until this point, we have been able to create email class that helps create email trigger and set-up email content and we have also been able to add our custom email to the WooCommerce Emails screen. We still need to define the email template that will be used for the email content. The paths to the email content templates have already been defined in the custom email class we created above. Let’s add the actual content that will be used by the welcome email we send out to customers.
Add the following code to crwc-welcome-email.php
file (inside emails
directory).
<?php /** * * Welcome email content template * * The file is prone to modifications after plugin upgrade or alike; customizations are advised via hooks/filters * */ if ( ! defined( 'ABSPATH' ) ) { exit; } /** * @hooked WC_Emails::email_header() Output the email header */ do_action( 'woocommerce_email_header', $email_heading, $email ); ?> <p><?php _e( 'Thank you for your purchase of Online training course. Your account has been successfully created over the Online Training Program portal.', 'woocommerce' ); ?></p> <p><?php _e( 'Use the following credentials to login to the portal:', 'woocommerce' ); ?></p> <p> <strong><?php __( 'Login URL: ', 'woocommerce' ) ?></strong><?php _e( 'https://example.com' ); ?><br /> <strong><?php __( 'Username: ', 'woocommerce' ) ?></strong><?php echo make_clickable( esc_attr( $order->billing_email ) ); ?><br /> </p> <p><?php _e( 'Below are the order details for your reference.' ) ?></p> <?php /** * @hooked WC_Emails::order_details() Shows the order details table. * @hooked WC_Emails::order_schema_markup() Adds Schema.org markup. * @since 2.5.0 */ do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email ); /** * @hooked WC_Emails::order_meta() Shows order meta data. */ do_action( 'woocommerce_email_order_meta', $order, $sent_to_admin, $plain_text, $email ); /** * @hooked WC_Emails::customer_details() Shows customer details * @hooked WC_Emails::email_address() Shows email address */ do_action( 'woocommerce_email_customer_details', $order, $sent_to_admin, $plain_text, $email ); /** * @hooked WC_Emails::email_footer() Output the email footer */ do_action( 'woocommerce_email_footer', $email );
You can modify the email content template as required based on your specific requirements and accordingly, the details that you’d need to send out to your customers.
Start making Custom WooCommerce email work
We’re almost there with starting to send custom WooCommerce emails to the customers. All we need to do is to trigger our code. The filter we added to include our custom email class to list of default email classes is placed in crwc-email-functions.php
, but hey!, wait a sec.. did we actually include it so that it is executed by WordPress? No, not yet. Let’s do that.
In order to do so, you can just add the following code to your theme’s functions.php
:
$upload_dir = wp_upload_dir(); include_once( $upload_dir['basedir'] . '/crwc-custom-emails/crwc-email-functions.php' );
That’s it! Now on, when a customer purchases an online training product from your store, he would receive a welcome email with his credentials. Sounds good, right? Happy customers, happy you!
This tut has been updated to work with WooCommerce 3.0+. Read how can you add and send custom WooCommerce email for WooCommerce version 3.0 and higher.
Hello thanks a lot for the post i’ll try to tweek it and create a ticket and send it as an email every time a customer buys an events ticket off my store. please advise if this post will help me achieve that many thanks in advance
Hi Dennis,
Sure, that is pretty much possible and this post guides you to do just that. Thanks.
Hello Aniket,
i tried out the code and it works like a charm thanks alot for the help but i still need a little bit of help
– i want to trigger the email if and only if the product ordered is of a specific category i.e. tickets category
– is it possible to send this email to more than one recipient??
Thanks a lot in advance
Hi Dennis,
Thanks for letting know that the tutorial was helpful. Regarding your queries:
trigger
function. You can use thewc_get_product_terms
function to retrieve all the categories assigned to the products in the order and then check if the product is assigned to category of your choice (tickets category in your case). To make it easy for you, here is the code that you can add to thetrigger
function in the email class (around line no. 83):Let me know if that helps you or if you face any issues. In case, if you find any difficulty in adding custom code and need any further assistance, you can contact us directly at support@cloudredux.com
Thanks.
Hi Aniket,
I am going to give this a shot to implement a review reminder by combining this with WP_CRON to send a review reminder some time after the shipping confirmed email goes out. I was just wondering why you put the code under wp-content/uploads rather than in a child them or a plugin?
Cheers,
Phil
Hi Phil,
Thanks for putting up that question. For sure, any customization that you want to add to your site should always be placed in a plugin or a child theme (if you have the flexibility to do so, with your theme) and the tutorial already mentions that. This tutorial and the code, however was not intended to work with a specific plugin or theme and hence, it guides you to create custom directories/files in the uploads directory and add all customization to those files. This will also make sure that the customization is not lost even when a theme is switched or alike.
Please feel free to add this code to a plugin or a child theme if that works best for you.
I’m not sure why but this didn’t work right off the bat for me. I had to make a copy of the email template in the woocommerce email templates folder. Before doing that, I was getting an error message that the template doesn’t exist. Would you know why?
Overall, this article has been really helpful! Thanks a lot for sharing!
Hi Steph,
The article explains setting-up custom WooCommerce email using custom files and directories under
uploads
directory. The main email class declares and rewrites the template base to allow using the template from uploads directory. If you follow the First Things First section and create all the required files and directories, the solution should work out-of-the-box as given in the tutorial.The error message that you mentioned would show up in a case when the required file is not found in the template base path specified. Hope that answers your question.
HI, I have some issue relaed with email. Since I am using Dokan multivendor manage plugin and it sends all sub order mail to customer. Any idea, how can i stop sending all mail. I just need to send main order only.
Hi Navas,
Unfortunately, your question involves another plugin which will require us to have information from your side before we actually could suggest you something. Also, your question falls out of scope of this tutorial. You can email us at hello[at]cloudredux.com with your queries and details of what you want to achieve and we’ll be glad to help. Thanks.
Hello Aniket and thank you for the great guide.
I have a question here regarding your guide.
What If I have create a custom order status?
My custom order status is called wc-order-confirmed and triggers when our customer service calls our customer and verified their shipping addresses.
How can I register a new trigger like
add_action( ‘woocommerce_order_status_on-hold_to_order-confirmed’, array( $this, ‘trigger’ ) );
Hi Christos,
I believe, you already have the custom order status implementation added to your website in a standard-compliant way. In that case, using a custom hook to check if the order status was changed from
pending
towc-order-confirmed
should work fine. Just that, if you have not already done so, you’ll need to register your custom hook to the list of defaults that WC uses to send transactional emails.You can use the following code to register your custom hook, which can later be used to trigger the custom email:
Once you have added this, you can use any one or all of the above action hooks in
class-crwc-welcome-email.php
file (as in the tutorial) as required to trigger the custom email.Let me know how does that work for you and if you still have any questions. Thanks.
Hello – thanks for the excellent tutorial. Everything works as expected except I am unable to add a new trigger via woocommerce_email_actions as you suggested to Christos.
function new_woocommerce_email_actions( $actions ){
$actions[] = ‘woocommerce_customer_save_address’;
return $actions;
}
add_filter( ‘woocommerce_email_actions’, ‘new_woocommerce_email_actions’ );
The action never triggers when I try to use it in my extended WC_Email class. Any suggestions or pointers?
Thanks for the detailed tutorial! I’m fairly new at this, so please excuse the dumb questions 🙂 I’m trying to edit this to send out an email a day after a customer’s booking. I’m guessing that I need to edit the trigger function, any guidance on where to start? I’m also using the woocommerce bookings plugin. I believe that I can get the booking date from it, and trigger the email based on that date? Thank you,
Hi Nate,
What you intend to achieve is very much possible but quite complex than what it appears to be. WooCommerce Bookings uses a booking start and end date for a customer booking and it is totally different and separate from the WooCommerce order statuses (processing, complete, etc.). I’m afraid, this tutorial does not cover that broad implementation.
Ideally, one way to implement custom booking email that you want to send out, is to schedule a cron event that triggers the email after the booking date specified in an order. That would require more code and few more tweaks. However, there is also an official WooCommerce extension Follow Ups that allows you to send out follow-up emails and is also fully compatible with WooCommerce Bookings.
Using the official extension should make your life really easy setting up the custom email for bookings. You can also choose to allow us to help you out with your custom requirements by sending us an email at hello[at]cloudredux.com and we’d be glad to help you with this. Thanks.
Very nice example. Umm actually in my case i want to send emails to multiple receipts as per product purchased. There is possibility that customer can buy variable product and simple product in single checkout. In such case i need to check send email when it is variable product. In simple words i’ll have to check the order and check products of the order and only send email with detail of variable product but not for simple product. How can i achieve that with some tweaks in the above example?
Hi Rafin,
That should be fairly easy. The part of this tutorial which describes the sending of the email in
class-crwc-welcome-email.php
can be tweaked a bit to achieve what you’re looking for.After line number 73, you may want to loop into the
$order_items
array, which would allow you to fetch the product data for each item in the order. See below:This way you’ll have access to all the variation data using the ID of the variation, and you should be able to send out the welcome email as desired. Hope that helps. Thanks.
Also when i tried with that code suddenly all of the emails settings in woocommerce -> emails tab gone. I removed code but it didnot returned to previous state. Hows that?
In that case, you probably missed on something really big, while trying to add / remove this code snippet. I really hope you did take a backup of things before starting to customize.
The only possible glitch I could think of, that possibly caused that issue is the usage of this filter
add_filter( 'woocommerce_email_classes', 'crwc_custom_woocommerce_emails' );
. If you still have this filter somewhere in your customization files, remove this and try loading the WooCommerce Emails screen again to see if all the other email configuration options are restored.Let me know if that helps. Thanks.
Dear Aniket,
+1 & 10/10, a very clear tut which easy on newbie’s.
I was want to personalise the email by address Dear Full Name. but could not get a handle to plug in the {billing_first_name} . ” ” .{billing_last_name}. if you could help it would be great. sorry if this dumb question, if you guide where to read more, that would also help.
from the template
to this one
<?php _e( 'Dear Full Name, Thank you for your purchase of Online training course. Your account has been successfully created over the Online Training Program portal.’, ‘woocommerce’ ); ?>
Hi Srikanth,
Glad to know that you liked the tutorial. In order to address the customer using his full name in the email that is sent out, you can add the following line (after line number 17) in
crwc-welcome-email.php
:get_formatted_billing_full_name()
is a WooCommerce native function that helps to retrieve billing user’s full name.Let me know if that helps you. Cheers!
Hi Aniket,
Great work with this tutorial! However I am facing an issue on getting the order items which causes:
PHP Fatal error: Call to a member function get_items() on null
this error happens on:
// get order items as array
$order_items = $this->object->get_items();
any insights? I am using WooCommerce 2.6.8 btw, could this be a compatibility problem?
Cheers
Hi Shariq,
Based on the information you provided, it seems like the
order ($this->object)
data in your case is empty. Maybe, you need to check on the order ID you provided in the previous line on that class method. Only in such a case, the error you reported would be generated. This is not a case of WC version incompatibility, since this has been tested with WC 2.6.8 too.Let me know if that helps. Thanks.
Why do you tell people to put the files under the ‘uploads’ directory?
If it was put in wp-content/plugins and the crwc-email-functions.php given the appropriate plugin comments at the top then people could activate it without having to edit their functions.php file.
That aside, I will be trying the code so that I can send a second ‘New order’ email that only lists the products order, leaving out customer info and prices. This will be for the stock picker.
Hi Damien,
You can definitely use the code snippet shared in the article and build a plugin or a structure around it that you like. I guess I have already mentioned it somewhere above. When sharing a how-to / guide on achieving something like this, it brings in a lot of caveats on what approach should ideally be shared in the tutorial being written. I chose to share it as a step-by-step guide on how-to create and send custom WooCommerce email(s) and so is the way it is.
Keeping it inside
uploads
directory was done to make sure there are no restrictions on what WP theme the tutorial uses and what the user could possibly use. I personally advocate compiling all of this into a plugin and then using it on your website. This will make sure you have all the related customization at one place and also make it independent of a theme or custom files.Hope you find the tutorial easy and that it helps you send out the additional email to customers as desired. Thanks!
Hello,
That is a very comprehensive guide but I don’t seem to be able to make it work.
I follow all the instructions here but the email does not appear on my wc emails list. Any idea why?
Hi Alexandros,
If everything has been used as-is, I don’t see a reason the custom email does not appear in WC Emails list. The only reason this could be happening is placement of the code in your theme/plugin file(s). I’m afraid you’ll have to troubleshoot this at your end, carefully following the instructions in the article, especially the First things first! section.
Hello Aniket
Many Thanks for the Tutorial. I am absolutely new and trying to achieve what is explained in the Tutorial. I am trying to send an email specific to courses that we will offer.. When someone purchases a course, this should trigger an email which will contain the links (5 – 6 in number) to join the specific course online.
These links should go with the email, when the course is purchased. Is it possible with the code you have specified in the tutorial above.
Thanks a ton
Sudip
Hi Sudip,
The tutorial helps you achieve just that. Just put in the links to the courses, you want to send out to the customers in the email template and add in the necessary conditionals if and as required and that should be it. Cheers!
Hello,
I’m having an issue changing the name of the class to one that corresponds to my website.
If I use class CRWC_Welcome_Email in the two files (the one for the class and one for the functions) the email works just fine
If I try to replace the name of the class with something that is in coherence with my needs (for example, use CSGR_Welcome_Email) it will not work. I tried to change all mentions of the class but I still get the same outcome. Could you tell me what I’m missing?
Hi Alexandros,
Changing the namespace for classes should work flawlessly. For the case you mentioned, you will need to change the class name from
CRWC_Welcome_Email
toCSGR_Welcome_Email
inclass-crwc-welcome-email.php
(line no. 11) andcrwc-email-functions.php
(line no. 12) and that should do.Let me know if that works for you. Thanks!
I have followed you tutorial and i am successfully added email in email settings.Now i want to add status as confirmation_for_COD.When user put an order with cash On delivery mode,the status should be set to confirmation_for_COD.Currently it’s setting to Processing.
I am new to wordpress. Sorry If I am asking irrelevant question
Hi Shruti,
I’m afraid that this tutorial does not actually cover what you are trying to achieve. The tutorial above helps you to send out an additional email for an order. If you want to change the default behavior of WooCommerce to set the order status to something else instead of Processing, you need to tweak the order status in the payment gateway class that handles the payment processing.
This tutorial may help you: http://www.remicorson.com/change-the-orders-default-status-by-gateway-in-woocommerce/. BTW, there is no order status as
confirmation_for_COD
as you mention; instead you could set the order status to On-Hold by following the tutorial I shared.Hope that helps.
Hi,
Thank you for very good and very clear tutorial.
I’m fairly new at this, so please excuse the dumb questions ? I’m trying to edit this to send out an email when I change the order status to “wc-shipped” , I already added a new status order with the plugin Custom Order Status for WooCommerce. I want to know how I can make to send an email with the order number and information to inform the Customer that his item has been shipped like ” Dear Mr…. your order number…. has been shipped!! ” when I change the order status on woocommerce.
I have to add a new email Template?
and a new action?
I don’t know how I can do this.
Thank for your help…
Hello Joy,
Yes, you will need to use a different action hook to trigger the desired email. The file
class-crwc-welcome-email.php
referenced in the tutorial above, includes the email triggers on line number 42 to 47. You will need to use different action hooks as per your requirement and then update the email content in filecrwc-welcome-email.php
based on what you would like to send out to your customers when they receive this email.Say, your custom order status was registered as
wc-shipped
(be very careful with the slug / id used to register the custom order status) and you would like to send out an email notifying users about the shipment of the product they purchased, you could use an action hook like this:Please note, that the above action hook would work when the order status changes from Processing to Shipped. This would largely depend on your WooCommerce store configuration for handling the payment and confirmation of orders. If you set the status to complete once an order is received on your store, you will need to use an action hook that triggers after the order status is changed from Complete to Shipped and likewise.
Let me know if that helps. Thanks!
Hi,
Would it be possible to use this option to send emails using dynamic values?
For instance, I have added a custom metabox to the order details page called ‘Delivery Delayed’ under which is a field with a datepicker.
Is there a way to insert the date from the field into an email before it gets sent?
Hi Keith,
Sure, that is as easy as using a custom field to get value and display on the frontend in WordPress. In the email template, you would just need to fetch the value of the custom field that you created and then output it the way you like.
You could use something like,
in the
crwc-email-functions.php
file to get the value of the custom field for an order and display in the template as desired.Let me know if that helps. Thanks!
On line 43 of the class above what is the naming convention to determine the name to use?
For instance I have created 2 new statuses called wc-order-in-progress and wc-order-completed.
I want to send out an email when the status is changed from ‘in progress’ to ‘completed’.
Does that mean the action should be called ‘woocommerce_order_status_order-in-progress_to_order-completed’?
At which point do you use an underscore or a dash?
Hi Keith,
If the order statuses that you registered use slugs as
wc-order-in-progress
andwc-order-completed
, the right action hook to trigger an email when the order status changes from in-progress to completed would bewoocommerce_order_status_wc-order-in-progress_to_wc-order-completed
.The WooCommerce action hook in the custom email class is supposed to follow this syntax:
woocommerce_order_status_{status_transition_from}_to_{status_transition_to}
and it should always use the exact order status ID / slug (in case of custom order status too).Hope that helps. Let me know if that works for you. Thanks!
Thank you very much for your help.
I have a question, I can not add the code to my functions.php because it make my check out page “internal server error”
do you have an other code? to add the email to woocommerce setting email?
Thank you
Hi Joy,
Did you try adding this code to the theme’s main
functions.php
? If you can, enable debugging in WordPress by addingdefine( 'WP_DEBUG', true )
inwp-config.php
and check if you are able to catch any errors on the Checkout page. Also, you could try viewing your server logs (error.log
or something similar) to find out the root cause of this issue.Other than the theme’s
functions.php
, you could use a plugin like WP Designer to add and execute custom code out of your theme’s core files. In that case you will just need to make sure that you change the URLs and paths of files / directories wherever they are being included in the tutorial above.Let me know if that answers your question. Thanks.
Very nice turtorial!
I’m having an issue with getting it to work in a multisite environment.
Added everything just like the “First things first” states, but I don’t get the “Welcome Email” in the list at Woocommerce -> Settings -> Emails.
Any suggestions?
Hi Aniket,
Thanks for the Tutorial. i have created custom registration page . how to send woo-commerce welcome template on custom registration page template.
Nice article.
With your code understanding I have written this plugin for custom order status & custom email which is with woocommerce v3.0 supported.
Hope it will help some.
ref: https://github.com/smartameer/wc-custom-orderstatus
Hi Aniket,
Great tutorial with simple explanation.
I tried this email option showing in woocommerce setting but didn’t received email.
I check error log file. Unable to understand what went wrong.
I got following error,
PHP Fatal error: Uncaught Error: Call to undefined method CRWC_Welcome_Email::email_heading() in /home/personaltutorco/public_html/wp-content/uploads/crwc-custom-emails/class-crwc-welcome-email.php:120
Stack trace:
#0 /home/personaltutorco/public_html/wp-content/plugins/woocommerce/includes/emails/class-wc-email.php(504): CRWC_Welcome_Email->get_content_html()
#1 /home/personaltutorco/public_html/wp-content/uploads/crwc-custom-emails/class-crwc-welcome-email.php(100): WC_Email->get_content()
#2 /home/personaltutorco/public_html/wp-includes/class-wp-hook.php(286): CRWC_Welcome_Email->trigger(1419)
#3 /home/personaltutorco/public_html/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array)
#4 /home/personaltutorco/public_html/wp-includes/plugin.php(453): WP_Hook->do_action(Array)
#5 /home/personaltutorco/public_html/wp-content/plugins/woocommerce/includes/class-wc-order.php(121): do_action(‘woocommerce_pay…’, 1419)
#6 /home/personaltutorco/public_html/wp-content/plugins/woo-instamojo/payment_confirm.php(51): W in /home/personaltutorco/public_html/wp-content/uploads/crwc-custom-emails/class-crwc-welcome-email.php on line 120
Hi Prasad,
That should be working just fine if you are correctly extending the
WC_Email
class as in the tutorial. Also, additionally can you let me know what version of WooCommerce are you using, details about what are you trying to accomplish with the tutorial or any other details that you think could help me in assisting you with this? Thanks.Hi, I have exactly the same problem. Woocommerce version 2.5.5. I did try the Woo 3 code just in case, also getting an Internal Server Error.
My error log is pretty much the same as Prasad’s.
Thanks in advance!
The script needs to be updated for the latest version of woocommerce. Like $this->object->billing_email needs to be replaced with $this->object->get_billing_email().
The same goes for the email heading:
Replace
$this->email_heading(…
with
$this->get_heading(…
Hi
Thanx for a great tutorial
How do i display a custom product field (‘myPrice’) in this custom email???
Regards, Henriette from Denmark
Hi Henriette,
That is as easy as displaying value of any other post custom (meta) field in WordPress. You can use the function get_post_meta to retrieve the value of your custom field for the product. All you need to be wary about is using the right product ID and the custom field name/key.
Let me know if that helps. Thanks.
Hello, if I want to send the email to my customer what should I do?
Thanks for your help!
Hi Aniket,
I want to know weather it is possible to send custom order completion emails for each product if I order multiple product in a single order.
Hi Praveen,
That is pretty much possible, but would just need additional customization. If not a major change, a simple
if-else
conditional could be used in the email template to load some content when product ID is something and likewise.If you need any specific help, feel free to contact us at <support(at)cloudredux.com> and we will be glad to provide you any assistance you may need.
Hi Aniket,
Such an awesome tutorial. I was wondering if it is possible to send out emails few days after the order status is changed to complete. Like mails regarding how to use or usage tips in order to educate the customer.