当前位置: 首页 > 知识库问答 >
问题:

当客户更改地址时发送通知电子邮件WooCommerce

祁建明
2023-03-14

关于这个线程的后续:Woocommerce退款电子邮件
我有一个问题我似乎无法解决,我阅读了所有关于扩展WC_Email类的主题,该类具有自定义触发器,但我无法使其工作。

我想要实现的是添加一个电子邮件通知到WooCommerce电子邮件设置,并发送一封电子邮件给收件人,只要客户更新它的帐单或运输细节。

因此,在functions.php中,我将woocommerce_customer_save_address操作添加到woocommerce_email_actions中,并添加我自己的WC_Email类扩展,如下所示:

<?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;
} 

这是自定义WC_Customer_Address_Change_Email类中的代码:

<?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();

类本身可以工作,因为额外的电子邮件通知设置显示在WooCommerce设置中。
问题出在触发器调用中:

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

就是不开火。当我将此更改为:

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

并在后端完成一个订单,我确实会从类中获得一个附加的电子邮件,并带有我自己的电子邮件模板。

@birgire建议的将woocommerce_customer_save_address操作添加到woocommerce_email_actions的解决方案似乎不起作用?

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

对此有什么想法或变通办法吗?
可能值得一提,我正在与小贩主题的儿童主题工作,但这不应该?我正在使用Woocommerce 2.3.11和Wordpress 4.2.2

谢谢!

更新08-07-2015

我找到了解决办法。在functions.php中,我直接在woocommerce_customer_save_address操作中使用扩展的WC_Email类发送邮件:

// 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() );
}

这样,我仍然可以从后端使用自定义类设置,并使用WC_Email类功能。

唯一不起作用的是,如果我将电子邮件类型设置为plain,Content-Type仍然设置为:text/html而不是text/plain,这会导致所有的换行符在模板中消失。在默认的WooCommerce电子邮件通知中,这确实可以正常工作。

我的猜测是$mailer->send调用中的$my_class->get_headers()参数不能正常工作。

有什么修复方法吗?
我可以在模板中手动设置内容类型吗?
谢谢

共有1个答案

拓拔玺
2023-03-14

几周前,我为我的一个客户写了一个简单的插件,几乎就是为了这个目的。每当客户更改/更新其帐户数据时,这将发送电子邮件通知。

查阅我的代码,并从那里编辑/删除任何你不需要的东西。

 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(); 
} 
 类似资料:
  • 我正试图阻止woo-commerce发送邮件时,订单状态更改。这些订单是亚马逊的,我的插件从亚马逊同步到Woo-Commerce。在这样做的时候,亚马逊和woo-commerce的邮件都收到了,这激怒了客户。因此,我想停止电子邮件功能停止时,状态从我的插件更改。要更改状态的代码是 有没有可以设置的标志来避免发送邮件? 任何种类的帮助都是非常感谢的。

  • 我有一个托管在Git存储中的项目(现在更名为比特桶服务器)。它是用詹金斯构建的。现在我在本地安装Git时打错了。比如@ab.com而不是@abc.com 每次构建后,詹金斯都会发送电子邮件通知,它会从Git提交中获取我不正确的电子邮件地址,并尝试发送它。 即使我在本地Git中更改了电子邮件地址,我仍然看到詹金斯将电子邮件发送到旧的错误地址。 我该怎么解决这个问题?

  • 这个代码应该发送一封电子邮件到指定的地址,当我硬编码“文本” 努力编码的文本和主题,它发送罚款!注意:使用GMAIL时,您必须启用不太安全的应用程序!

  • 在以前的Woocommerce版本中,当订单从挂起状态更改为取消状态时,会自动发送电子邮件通知(在我的例子中,这发生在admin的inventory部分中设置的分配时间之后)。 在WooCommerce 3.0.8中,他们删除了这种自动化,并将其标记为修补程序:https://github.com/WooCommerce/WooCommerce/blob/master/changelog.txt

  • 当WooCommerce中的订单状态从pending设置为processing时,我可以向我的客户发送电子邮件。但我需要发送一个电子邮件只到一个自定义电子邮件adresse而不是客户默认地址。 文件有钩子或过滤器吗?

  • 我已经在我的WooCommerce回拨订单()中创建了自定义订单状态: 现在我想收到一封电子邮件,每当收到一个订单,已经给出了状态栏。我根据这篇有用的文章创建了一个插件:如何添加自定义的WooCommerce电子邮件 这个链接包含我的插件源代码和functions.php代码。 我在function.php中添加了钩子: 当订单更改为“延迟订单”状态时,不会发生任何情况。 有什么想法吗? 我已经尝