1

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

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.

1 Answer 1

3

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');
2
  • You are right, when you call mailer() it creates the instance of class WC_Emails. so then only you can access methods inside the class using the instance. +1 for the try
    – sun
    Commented Jun 23, 2014 at 10:16
  • You can also just do: $mailer = WC()->mailer(); Commented Feb 10, 2016 at 22:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.