Using woocommerce email header and footer in custom email
Let say you want to send your customer a custom email that not triggered by conventional action (Order status change, user password change) , or basically email not listed in this ‘/wp-admin/admin.php?page=wc-settings&tab=email’ page, but would like to use the Woocommerce email header and footer template. You can use the following script.
function send_custom_email($user_id){
$subject = "This is a custom email subject";// email subject
$user_info = get_userdata( $user_id );//get the customer data ,if you have the user id here.
ob_start(); //using buffer if you have long email .
wc_get_template( 'emails/email-header.php', array( 'email_heading' => "This is email heading" ) ); // this is to insert the woocommerce email header, and in the array 'email_heading' is the email heading content.
echo '<p>Hi, '.$user_info->display_name.',</p> <p>This is an example of the email content....</p>';
//echo the email content here. Note that you can have the customer data if you got the user info.
wc_get_template( 'emails/email-footer.php' );//insert the woocommerce email footer.
$message = ob_get_clean(); //output the content as a whole from the buffer.
$to = $user_info->user_email; //to the customer email or you have specific email
wc_mail( $to, $subject, $message);
}
The above function is to demonstrate how you can insert the woocommerce email’s header and footer inside a custom email template.
In the echo
content part you can also use wc_get_emplate();
to your plugin/theme directory that have your custom email content.
Also note that this send_custom_email
function is not call by any action and accept 1 single User ID
as a function argument. You can use this function to any WordPress or Plugin hook.