Woocommerce custom emails without header and footer

I’m working on WordPress / WooCommerce extension plugin to enable Password Reset on the main site (not wp-admin). I want to use standard WooCommerce mailer and email template to send Password Reset Request email with Activation link

Email is triggered by function

Read More
function send_activation_link() {
  global $woocommerce;

  if ( 'POST' !== strtoupper( $_SERVER[ 'REQUEST_METHOD' ] ) )
    return;

  if ( empty( $_POST[ 'action' ] ) || ( 'lost-password' !== $_POST[ 'action' ] ) )
     return;

  //My other code here....

  ob_start();

  // Get mail template
  woocommerce_get_template('emails/customer-reset-password-link.php', array(
    'user_login'    => $user_login,
    'blogname'      => $blogname,
    'email_heading' => $email_heading,
    'key'           => $key
  ));

  // Get contents
  $message = ob_get_clean();

  $mailer = $woocommerce->mailer();
  $mailer->send( $user_email, $subject, $message);
}
add_action( 'init', 'send_activation_link');

The template emails/customer-reset-password-link.php:

<?php
/**
 * Password reset email
*/
if (!defined('ABSPATH')) exit; 
do_action('woocommerce_email_header', $email_heading); 
?>
<p>Message to user</p>
<?php 
do_action('woocommerce_email_footer'); 
?>

It works well, but the email isn’t wrapped with header and footer, just Message to user

If I amend WC_Email class with a function:

function tb_send_activation_link($user_login, $user_email) {

  //My other code here....

  ob_start();

  // Get mail template
  woocommerce_get_template('emails/customer-reset-password-link.php', array(
    'user_login'    => $user_login,
    'blogname'      => $blogname,
    'email_heading' => $email_heading,
    'key'           => $key
  ));

  // Get contents
  $message = ob_get_clean();

  $this->send( $user_email, $subject, $message, $headers, $attachments );
}

And then change function in my plugin to:

function send_activation_link() {
  global $woocommerce;

  //My other code here....

  $mailer = $woocommerce->mailer();
  $mailer->tb_send_activation_link($user_login, $user_email);
}
add_action( 'init', 'send_activation_link');

It sends complete email (wrapped with header and footer).

Have no idea what I’m doing wrong and how to send emails from my plugin. Don’t really want to change core WooCommerce code and thus would really appreciate any advice how to solve it.

In advance, thank you.

Related posts

Leave a Reply

1 comment

  1. Seems I’ve managed to find the solution. $mailer = $woocommerce->mailer(); must be called before the Get mail template

    function send_activation_link() {
      global $woocommerce;
    
      //My other code here....
    
      $mailer = $woocommerce->mailer();
    
      ob_start();
    
      // Get mail template
      woocommerce_get_template('emails/customer-reset-password-link.php', array(
        'user_login'    => $user_login,
        'blogname'      => $blogname,
        'email_heading' => $email_heading,
        'key'           => $key
      ));
    
      // Get contents
      $message = ob_get_clean();
    
      $mailer->send( $user_email, $subject, $message);
    }
    add_action( 'init', 'send_activation_link');