← Blog

Send WooCommerce emails using code – useful for testing custom emails

WordPress · WooCommerce1 min read

If you need to test custom WooCommerce checkout email templates or functionality you don’t want to constantly be generating orders, or changing order statuses.

Instead use this snippet to send the emails until your code is working perfectly.

Just add it to your theme functions.php.  Be sure to set the $order_id and the $email_class if necessary.

add_action('init', 'send_wc_email');
function send_wc_email() {

// The order ID we're testing the email with
$order_id = 623;

// The email we want to send
$email_class = 'WC_Email_Customer_Processing_Order';

/*
Available emails are:
WC_Email_Customer_Note
WC_Email_Customer_Note
WC_Email_Customer_Completed_Order
WC_Email_Customer_Completed_Order
WC_Email_Customer_New_Account
WC_Email_Customer_New_Account
WC_Email_Customer_Reset_Password
WC_Email_Customer_Reset_Password
WC_Email_Customer_Processing_Order
WC_Email_Customer_Processing_Order
WC_Email_Customer_Invoice
WC_Email_Customer_Invoice
WC_Email_New_Order
*/

// Load the WooCommerce Emails
$wc_emails = new WC_Emails();
$emails = $wc_emails->get_emails();

// Select the email we want & trigger it to send
$new_email = $emails[$email_class];
$new_email->trigger($order_id);

// Show the email content
echo $new_email->get_content();
}

Stuck on something like this?

I help businesses and agencies with WordPress, WooCommerce and JavaScript problems, from one-off fixes to ongoing support.