Send notification email when customer changes address WooCommerce

Following up on this thread: Woocommerce Refund Email
I have a problem I can’t seem to fix, I read all the topics out there regarding extending the WC_Email class with a custom class with a custom trigger but I can’t get it to work.

What I would like to achieve is to add an email notification to the WooCommerce Email settings and send an email to a recipient whenever a customer updates it’s billing or shipping details.

Read More

So in my functions.php I add the woocommerce_customer_save_address action to the woocommerce_email_actions and add my own WC_Email class extension like this:

<?php    
// Add a custom email action to the list of emails WooCommerce should load
add_action( 'woocommerce_init', function() { 
  add_action( 'woocommerce_customer_save_address', array( WC(), 'send_transactional_email' ), 10, 10 ); // WC 2.2-
});
add_filter( 'woocommerce_email_actions', 'add_customer_address_change_woocommerce_email_actions' ); // WC 2.3+
function add_customer_address_change_woocommerce_email_actions( $email_actions ) {
  $email_actions[] = 'woocommerce_customer_save_address';
  return $email_actions;
}
// Add a custom email class to the list of emails WooCommerce should load
add_filter( 'woocommerce_email_classes', 'add_customer_address_change_woocommerce_email_classes' );
function add_customer_address_change_woocommerce_email_classes( $email_classes ) {
    require_once( get_stylesheet_directory() . '/includes/class-wc-customer-address-change-email.php' );
    $email_classes['WC_Customer_Address_Change_Email'] = new WC_Customer_Address_Change_Email();
    return $email_classes;
} 

This is my code in my custom WC_Customer_Address_Change_Email class:

<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

if ( ! class_exists( 'WC_Customer_Address_Change_Email' ) ) :

/**
 * A custom address change WooCommerce Email class
 *
 * @class       WC_Customer_Address_Change_Email
 * @version     2.3.0
 * @package     WooCommerce/Classes/Emails
 * @author      WooThemes
 * @extends     WC_Email
 */
class WC_Customer_Address_Change_Email extends WC_Email {

  public $woocommerce;
  public $current_user;

    /**
     * Set email defaults
     *
     * @since 0.1
     */
    function __construct() {

        // set ID, this simply needs to be a unique name
        $this->id = 'wc_customer_address_changed';

        // this is the title in WooCommerce Email settings
        $this->title = __('Customer Address Change', 'monum');

        // this is the description in WooCommerce email settings
        $this->description = __('Address Change Notification emails are sent when a customer changes his billing or shipping address', 'monum');

        // these are the default heading and subject lines that can be overridden using the settings
        $this->heading = __('Customer Address Change', 'monum');
        $this->subject = __('Customer Address Change', 'monum');

        // these define the locations of the templates that this email should use, we'll just use the new order template since this email is similar
        $this->template_html  = 'emails/admin-address-change.php';
        $this->template_plain = 'emails/plain/admin-address-change.php';

        // Trigger on new paid orders
        //add_action( 'woocommerce_order_status_completed_notification', array( $this, 'trigger' ) );
        add_action( 'woocommerce_customer_save_address', array( $this, 'trigger' ) );

        // Call parent constructor to load any other defaults not explicity defined here
        parent::__construct();

        // this sets the recipient to the settings defined below in init_form_fields()
        $this->recipient = $this->get_option( 'recipient' );

        // if none was entered, just use the WP admin email as a fallback
        if ( ! $this->recipient )
            $this->recipient = get_option( 'admin_email' );
    }


    /**
   * trigger function.
   *
   * @access public
   * @return void
   */
    function trigger( $user_id ) {

        $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
    }      

    /**
     * get_content_html function.
     *
     * @access public
     * @return string
     */
    function get_content_html() {
        ob_start();
        wc_get_template( $this->template_html, array(
            'email_heading' => $this->get_heading(),
            'blogname'      => $this->get_blogname(),
            'sent_to_admin' => true,
            'plain_text'    => false
        ) );
        return ob_get_clean();
    }


    /**
     * get_content_plain function.
     *
     * @access public
     * @return string
     */
    function get_content_plain() {
        ob_start();
        wc_get_template( $this->template_plain, array(
            'email_heading' => $this->get_heading(),
            'blogname'      => $this->get_blogname(),
            'sent_to_admin' => true,
            'plain_text'    => true
        ) );
        return ob_get_clean();
    }


    /**
     * Initialise Settings Form Fields
   *
   * @access public
   * @return void
     */
    function init_form_fields() {

        $this->form_fields = array(
            'enabled' => array(
        'title'         => __( 'Enable/Disable', 'woocommerce' ),
        'type'          => 'checkbox',
        'label'         => __( 'Enable this email notification', 'woocommerce' ),
        'default'       => 'yes'
      ),
            'recipient'  => array(
                'title'       => __( 'Recipient(s)', 'woocommerce' ),
                'type'        => 'text',
                'description' => sprintf( __( 'Enter recipients (comma separated) for this email. Defaults to <code>%s</code>.', 'woocommerce' ), esc_attr( get_option('admin_email') ) ),
                'placeholder' => '',
                'default'     => ''
            ),
            'subject'    => array(
                'title'       => __( 'Subject', 'woocommerce' ),
                'type'        => 'text',
                'description' => sprintf( __( 'This controls the email subject line. Leave blank to use the default subject: <code>%s</code>.', 'woocommerce' ), $this->subject ),
                'placeholder' => '',
                'default'     => ''
            ),
            'heading'    => array(
                'title'       => __( 'Email Heading', 'woocommerce' ),
                'type'        => 'text',
                'description' => sprintf( __( 'This controls the main heading contained within the email notification. Leave blank to use the default heading: <code>%s</code>.', 'woocommerce' ), $this->heading ),
                'placeholder' => '',
                'default'     => ''
            ),
            'email_type' => array(
                'title'       => __( 'Email type', 'woocommerce' ),
                'type'        => 'select',
                'description' => __( 'Choose which format of email to send.', 'woocommerce' ),
                'default'     => 'html',
                'class'       => 'email_type wc-enhanced-select',
                'options'     => $this->get_email_type_options()
            )
        );
    }


}

endif;

return new WC_Customer_Address_Change_Email();

The class itself works because the extra e-mail notification setting shows up in the WooCommerce settings.
The problem lies within the trigger call:

add_action( 'woocommerce_customer_save_address', array( $this, 'trigger' ) );

It just doesn’t fire.
When I change this to:

add_action( 'woocommerce_order_status_completed_notification', array( $this, 'trigger' ) );

And complete an order in the backend, I do get an additional email from the class with my own email template.

The suggested solution from @birgire to add the woocommerce_customer_save_address action to the woocommerce_email_actions doesn’t seem to work?

add_filter( 'woocommerce_email_actions', function( $email_actions ) {
    $email_actions[] = 'woocommerce_customer_save_address';
    return $email_actions;
});

Any thoughts or workarounds for this?
Might be worth mentioning I’m working with a child theme of the Peddlar theme but that shouldn’t? I am using Woocommerce 2.3.11 and WordPress 4.2.2

Thanks!


UPDATE 08-07-2015

I found a workaround for this.
In my functions.php I use my extended WC_Email class directly inside the woocommerce_customer_save_address action to send the mail:

// Send notification email when customer saves address using custom extended WC_Email class
add_action( 'woocommerce_customer_save_address','notify_admin_customer_address_change', 10, 2);
function notify_admin_customer_address_change( $user_id ) {
  global $woocommerce;
    $mailer   = WC()->mailer();
    $My_Class = new WC_Customer_Address_Change_Email; // retrieve custom extended class 
  $mailer->send( $My_Class->get_recipient(), $My_Class->get_subject(), $My_Class->get_content(), $My_Class->get_headers(), $My_Class->get_attachments() );
}

That way I can still use the custom class settings from the backend and use the WC_Email class functionality.

The only thing that isn’t working yet, if I set the email type to plain, the Content-Type is still set to: text/html instead of text/plain, causing all my line-breaks to disappear in my template. In the default WooCommerce email notifications this does work correctly.

My guess is the $My_Class->get_headers() parameter in the $mailer->send call doesn’t work correctly.

Any fix for this?
Can I set the Content-Type manually in the template itself?
Thanks

Related posts

1 comment

  1. A few weeks ago I wrote a simple plugin for one of my clients for almost this exact purpose.
    This will sends an email notification whenever a customer changes/updates his account data.

    Look up to my code and edit/remove anything that you don’t need from there.

     if( !class_exists('WooCommerceNotifyChanges') ){
    
    class WooCommerceNotifyChanges{
    
        function __construct(){
            // customer saves main account data
            add_action('woocommerce_save_account_details', array( $this, 'woocommerce_send_notification' ));
    
            // customer saves billing or shipping data
            add_action('woocommerce_customer_save_address', array( $this, 'woocommerce_send_notification' ));
        }
    
        function woocommerce_send_notification(){
            $body       = '';
            $to         = 'recipient@gmail.com';    //address that will receive this email
            $subject    = 'One of your customers has updated their information.';
    
            $curr_user      = wp_get_current_user();
            $user_id        = $curr_user->ID;
            $curr_username  = $curr_user->data->user_login;
    
            $body .= '<table>';
            $body .= '<tr><td><strong>Account</strong></td></tr>';
            $body .= '<tr><td>Username: </td><td>' . $curr_username                                         . '</td></tr>';
            $body .= '<tr><td colspan="2">&nbsp;</td></tr>';
            $body .= '<tr><td colspan="2"><strong>Billing</strong></td></tr>';
            $body .= '<tr><td>First name: </td><td>' . get_user_meta( $user_id, 'billing_first_name', true ). '</td></tr>';
            $body .= '<tr><td>Last name: </td><td>' . get_user_meta( $user_id, 'billing_last_name', true )  . '</td></tr>';
            $body .= '<tr><td>Company: </td><td>' . get_user_meta( $user_id, 'billing_company', true )      . '</td></tr>';
            $body .= '<tr><td>Address 1: </td><td>' . get_user_meta( $user_id, 'billing_address_1', true )  . '</td></tr>';
            $body .= '<tr><td>Address 2: </td><td>' . get_user_meta( $user_id, 'billing_address_2', true )  . '</td></tr>';
            $body .= '<tr><td>City: </td><td>' . get_user_meta( $user_id, 'billing_city', true )            . '</td></tr>';
            $body .= '<tr><td>Post code: </td><td>' . get_user_meta( $user_id, 'billing_postcode', true )   . '</td></tr>';
            $body .= '<tr><td>Country: </td><td>' . get_user_meta( $user_id, 'billing_country', true )      . '</td></tr>';
            $body .= '<tr><td>State: </td><td>' . get_user_meta( $user_id, 'billing_state', true )          . '</td></tr>';
            $body .= '<tr><td>Phone: </td><td>' . get_user_meta( $user_id, 'billing_phone', true )          . '</td></tr>';
            $body .= '<tr><td>Email: </td><td>' . get_user_meta( $user_id, 'billing_email', true )          . '</td></tr>';
            $body .= '<tr><td colspan="2">&nbsp;</td></tr>';
            $body .= '<tr><td colspan="2"><strong>Shipping</strong></td></tr>';
            $body .= '<tr><td>First name: </td><td>' . get_user_meta( $user_id, 'shipping_first_name', true ). '</td></tr>';
            $body .= '<tr><td>Last name: </td><td>' . get_user_meta( $user_id, 'shipping_last_name', true ) . '</td></tr>';
            $body .= '<tr><td>Company: </td><td>' . get_user_meta( $user_id, 'shipping_company', true )     . '</td></tr>';
            $body .= '<tr><td>Address 1: </td><td>' . get_user_meta( $user_id, 'shipping_address_1', true ) . '</td></tr>';
            $body .= '<tr><td>Address 2: </td><td>' . get_user_meta( $user_id, 'shipping_address_2', true ) . '</td></tr>';
            $body .= '<tr><td>City: </td><td>' . get_user_meta( $user_id, 'shipping_city', true )           . '</td></tr>';
            $body .= '<tr><td>Post code: </td><td>' . get_user_meta( $user_id, 'shipping_postcode', true )  . '</td></tr>';
            $body .= '<tr><td>Country: </td><td>' . get_user_meta( $user_id, 'shipping_country', true )     . '</td></tr>';
            $body .= '<tr><td>State: </td><td>' . get_user_meta( $user_id, 'shipping_state', true )         . '</td></tr>';
            $body .= '</table>';    
    
            //set content type as HTML
            $headers = array('Content-Type: text/html; charset=UTF-8;');
    
            //send email
            if( wp_mail( $to, $subject, $body, $headers ) ){
                //echo 'email sent';
            }else{
                //echo 'email NOT sent';                
            }
            //exit();
        }
    }
    new WooCommerceNotifyChanges(); 
    } 
    

Comments are closed.