Sending Custom WooCommerce Emails on Scheduled Events: A Step-by-Step Guide
As a WooCommerce store owner, you may sometimes need to send custom emails to your customers based on specific events or actions. One common use case is sending a follow-up email after a certain period of time following an order placement. This could be a thank-you email, a shipping notification, or a request for product feedback.
In this article, we'll walk you through the process of setting up a custom WooCommerce email that is triggered by a scheduled event. We'll cover the essential steps and address common pitfalls to help you get it right the first time.
Understanding the Basics
In order to send a custom WooCommerce email on a scheduled event, you'll need to follow these steps:
-
Create a Custom Email Class: You'll need to create a custom email class that extends the base WC_Email
class. This class will handle the content and delivery of your custom email.
-
Add an Action Hook: In the __construct()
method of your custom email class, you'll need to add an action hook that will be triggered by your scheduled event. This is where you'll tell WooCommerce to send the custom email.
-
Implement the Trigger Function: When the scheduled event occurs, you'll need to call the do_action()
function to trigger your custom email. This will invoke the action hook you set up in the previous step.
-
Schedule the Event: Finally, you'll need to set up a WordPress cron job to trigger the scheduled event at the desired interval.
Let's dive into the details of each step.
Step 1: Create a Custom Email Class
To create a custom WooCommerce email, you'll need to extend the base WC_Email
class and override the necessary methods. Here's an example:
class My_Custom_Email extends WC_Email {
public function __construct() {
$this->id = 'my_custom_email';
$this->title = __( 'My Custom Email', 'my-plugin' );
$this->description = __( 'This is a custom email sent on a scheduled event.', 'my-plugin' );
$this->template_html = 'emails/my-custom-email.php';
$this->template_plain = 'emails/my-custom-email-plain.php';
// Call the parent constructor
parent::__construct();
// Add an action hook that will be triggered by your scheduled event
add_action( 'my_custom_email', array( $this, 'trigger' ), 10, 1 );
}
public function trigger( $order_id ) {
if ( ! $order_id ) {
return;
}
$this->object = wc_get_order( $order_id );
if ( $this->object ) {
$this->recipient = $this->object->get_billing_email();
$this->find['order-date'] = '{order_date}';
$this->replace['order-date'] = wc_format_datetime( $this->object->get_date_created() );
$this->find['order-number'] = '{order_number}';
$this->replace['order-number'] = $this->object->get_order_number();
$this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
}
}
public function get_content_html() {
ob_start();
wc_get_template( $this->template_html, array(
'order' => $this->object,
'email_heading' => $this->get_heading(),
) );
return ob_get_clean();
}
public function get_content_plain() {
ob_start();
wc_get_template( $this->template_plain, array(
'order' => $this->object,
'email_heading' => $this->get_heading(),
) );
return ob_get_clean();
}
}
In this example, we've created a custom email class called My_Custom_Email
that extends the WC_Email
class. In the __construct()
method, we set the ID, title, and description of the email, as well as the HTML and plain-text templates.
We also add an action hook called my_custom_email
that will be triggered by our scheduled event. The trigger()
method is where we'll handle the actual email sending logic.
Step 2: Add an Action Hook
In the __construct()
method of your custom email class, you'll need to add an action hook that will be triggered by your scheduled event. This is where you'll tell WooCommerce to send the custom email.
public function __construct() {
// ...
add_action( 'my_custom_email', array( $this, 'trigger' ), 10, 1 );
}
In this example, we're adding an action hook called my_custom_email
that will trigger the trigger()
method of our custom email class. The 10
argument represents the priority of the action, and the 1
argument represents the number of parameters that the trigger()
method expects (in this case, a single $order_id
parameter).
Step 3: Implement the Trigger Function
When the scheduled event occurs, you'll need to call the do_action()
function to trigger your custom email. This will invoke the action hook you set up in the previous step.
function my_scheduled_function() {
global $woocommerce;
$mailer = WC()->mailer();
$order_id = 123; // Replace with the appropriate order ID
do_action( 'my_custom_email', $order_id );
}
In this example, the my_scheduled_function()
is the function that will be triggered by your WordPress cron job. Inside this function, we first make sure to include the necessary WooCommerce classes by calling global $woocommerce;
and $mailer = WC()->mailer();
.
Then, we call the do_action()
function, passing the 'my_custom_email'
action hook and the $order_id
parameter. This will trigger the trigger()
method of our custom email class, which will handle the actual email sending logic.
Step 4: Schedule the Event
Finally, you'll need to set up a WordPress cron job to trigger the scheduled event at the desired interval. You can do this using the wp_schedule_event()
function.
function my_schedule_custom_email() {
if ( ! wp_next_scheduled( 'my_scheduled_function' ) ) {
wp_schedule_event( time() + 86400, 'daily', 'my_scheduled_function' );
}
}
add_action( 'wp', 'my_schedule_custom_email' );
In this example, the my_schedule_custom_email()
function checks if the 'my_scheduled_function'
event is already scheduled. If not, it schedules the event to run daily (using the 'daily'
schedule) starting from the current time plus one day (86,400 seconds).
The add_action( 'wp', 'my_schedule_custom_email' )
line ensures that the scheduling function is called when the WordPress 'wp'
action is triggered, which is during the initial page load.
Get a Free AI Website Audit
Automatically identify UX and content issues affecting your conversion rates with Flowpoint's comprehensive AI-driven website audit.
Putting It All Together
To summarize, the key steps to sending a custom WooCommerce email on a scheduled event are:
- Create a custom email class that extends the
WC_Email
class and implements the necessary methods.
- Add an action hook in the
__construct()
method of your custom email class.
- In the function that is triggered by your WordPress cron job, call the
do_action()
function to trigger the custom email.
- Schedule the event using the
wp_schedule_event()
function.
By following these steps, you can easily set up a custom WooCommerce email that is triggered by a scheduled event, such as a follow-up email after a certain period of time following an order placement.
Remember, this is just one example of how you can use scheduled events to trigger custom WooCommerce emails. The possibilities are endless, and you can adapt this approach to suit your specific needs.
If you're looking for a more comprehensive solution to optimize your WooCommerce store's performance and conversion rates, consider checking out Flowpoint.ai. Flowpoint's AI-powered analytics and recommendations can help you identify and fix technical issues, improve your user experience, and boost your overall conversion rates