WooCommerce comes with several email templates out of the box. It also provides you with a way to register your own emails which are going to be sent on some events. In this tutorial we will learn how to create a new custom WooCommerce Email.
The Main WooCommerce Email Class
To create a custom WooCommerce Email, you should understand the main
WC_Email class. You can find it at
includes/emails/class-wc-email.php or read it online on GitHub. This class is extending the
WC_Settings_API class which is used for any settings related things in WooCommerce.
I won’t go into every possible detail of the
WC_Email class (I leave that for the ambitious ones), but there are some of the attributes and methods you might want to know:
$title– Email title, used in admin,
$description– Email description, used in admin,
$heading– Email heading, used in templates,
$subject– Email subject,
$template_base– the absolute path to the folder where the templates are located,
$template_html– the relative path to the HTML email template,
$template_plain– the relative path to the plain email template,
$recipient– a string of recipient emails,
$object– The object for which is the email. This can be a WC_Product, WC_Customer or something else (even our own),
$customer_email– if this email is sent to customers.
send()– this method is used for sending the email (check the parameters),
init_form_fields()– this method is used to register form fields.
Creating a Custom WooCommerce Email
Let’s now jump right into creating a custom WooCommerce Email. If you’re looking to implement this into your own plugin, just replace the paths for your own plugin. I’ll create a new plugin folder
custom-wc-email and also a file inside of it
WooCommerce provides a lot of Customer Emails but your site only sends an email to the administrator or the provided recipient when an order is canceled. Such email is not sent to customers. We will create a custom WooCommerce email that will also notify the customer when an order was canceled.
Let’s create additional folders in our plugin. Create two folders
templates. We will also go one more step further and inside of the folder
templates create a new folder
emails. Then inside of that folder, create another one
templates will hold email templates inside of the subfolder
emails. Plain templates will be located under
Registering a Custom WooCommerce Email
To register a custom WooCommerce Email, we will create the main plugin class where we will hook into the WooCommerce emails and define the absolute path to the plugin folder.
For now, your site will not work if you have activated this plugin. That’s because we have yet to create the custom email.
Define the Custom WooCommerce Email
Create a file
class-wc-customer-cancel-order.php. This file will contain all the code we will create here. Let’s define the
__construct method first.
In the constructor method, we define the default values to attributes. We then hook on two actions where the order can be cancelled. When the order is cancelled, we will call the method
We are also setting the relative and absolute paths to templates.
trigger method is not an inherited method and we need to define it ourselves. This method will accept the Order ID which is passed through the action hooks. We will then get the order and use the billing email as a recipient email.
Overriding the Content Methods
We also need to override the content methods which are used to get the HTML and plain templates.
We are using the function
wc_get_template_html where we pass the relative path to the email templates. We also pass the absolute path to the folder where the path to email templates is located. Since the email templates that we are using are copied from the admin cancelled email, we are also passing the required parameters.
The last thing is to define the templates that are going to be used for creating the email content. The first template will be an HTML template. Create a file
With this hooks, WooCommerce will fill the required data since the hooks are the same as for the WooCommerce Admin Cancelled email. The plain template will go into
templates/emails/plain. We will use the same file name
Here is the complete code which you can download and test it on your own.
With one custom WooCommerce email (or more) you can enhance the user experience of your plugin. This can be emails on subscriptions, bookings, rentals or anything else. Some solutions won’t require such emails, but with this knowledge you can now enhance the experience of your own clients also or even suggest such custom emails where needed.
Have you ever tried working with custom WooCommerce emails? Do you maybe have an idea where such emails could be also used?