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

2024-04-09

跟进此线程:Woocommerce 退款电子邮件 https://stackoverflow.com/questions/25544762/woocommerce-refund-email/26413223
我有一个似乎无法解决的问题,我阅读了有关使用带有自定义触发器的自定义类扩展 WC_Email 类的所有主题,但我无法让它工作。

我想要实现的是将电子邮件通知添加到WooCommerce 电子邮件设置并在客户更新其账单或运输详细信息时向收件人发送电子邮件。

所以在我的函数.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_客户_地址_更改_电子邮件 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();

该类本身可以正常工作,因为额外的电子邮件通知设置显示在 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;
});

对此有什么想法或解决方法吗?
可能值得一提的是,我正在使用 Peddlar 主题的儿童主题,但这不应该吗?我正在使用 Woocommerce2.3.11和 WordPress4.2.2

Thanks!


UPDATE2015年8月7日

我找到了解决这个问题的方法。 在我的函数.php我直接在内部使用扩展的 WC_Email 类woocommerce_customer_save_address发送邮件的操作:

// 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 类功能。

唯一还不起作用的事情是,如果我将电子邮件类型设置为普通,内容类型仍设置为:文本/html代替文本/纯文本,导致我的所有换行符都消失在我的模板中。在默认的 WooCommerce 电子邮件通知中,这确实可以正常工作。

我的猜测是$My_Class->get_headers()中的参数$mailer->send呼叫无法正常工作。

有什么解决办法吗?
我可以设置内容类型手动在模板本身中?
Thanks


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

查看我的代码并从那里编辑/删除您不需要的任何内容。

 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         = '[email protected] /cdn-cgi/l/email-protection';    //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(); 
} 
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

当客户更改地址时发送通知电子邮件 WooCommerce 的相关文章

  • XP及PHP MYSQL 练级系统

    我已经查看了所有提出的问题和答案 但我似乎找不到最适合我的答案 我想做的是开发一个系统 当用户达到一定的 XP 限制时 系统会进入下一个级别 它显示了下一个 XP 之前需要多少 XP So lvl1 0 gt lvl2 256 gt lvl
  • 使用php在html页面中显示bbcode

    我已经有一个 bbcode 字符串 mybbcode b Hello word b 使用 php 我想在 html 页面中以 html 格式显示它 例如 div gt b hello word b div 基本上其他人已经对你说过了 但是如
  • 在 Drupal 中选择性地删除页面的样式表

    我正在尝试为首页制作不同的布局 在此过程中 我声明了名为 front page css 和 page front tpl php 的新样式表 我正在使用加载responsive sidebar css 的 Zen 子主题 我想删除 resp
  • TCPDF / FPDI 可以接受 PDF 作为字符串吗?

    是否可以将 TCPDF 或 FPDI PDF 作为字符串提供 我有一个传入的 PDF 数组作为字符串 但无法写入磁盘 我在文档中找不到与此相关的任何内容 如果没有 是否有一种有效的方法来从内存或作为对象存储 读取这些 PDF 将它们喂给 F
  • 如何在 PHP 中将默认日期设置为波斯日期?

    如何在 PHP 中将默认日期设置为波斯日期 说吧 如果我echo这个功能date Y m d 然后它会显示2018 03 05但我想要1396 12 14波斯日期 请检查 http php net manual en intldatefor
  • 使用 Unity 在 iOS 应用程序上发送电子邮件时出错

    我正在开展一个学校项目 我正在尝试记录从我统一制作的游戏中存储的数据 我的问题是我想通过电子邮件将数据发送到应用程序 一切正常 电子邮件与数据一起发送等 但是当我将应用程序构建到 iOS 设备时 它不会发送电子邮件 我在 Unity 中没有
  • PHP 从日志事件中获取行号

    好的 我还有一个问题HERE https stackoverflow com questions 3213423 php how could i make this class better suggestions feedback wel
  • php exec 返回的结果比直接进入命令行要少

    我有一个 exec 命令 它的行为与通过 Penguinet 给 linux 的相同命令不同 res exec cd mnt mydirectory zcat log file gz echo res 当将命令直接放入命令行时 我在日志文件
  • 同一路由组的多个前缀

    我正在为一所学校编写一个相当简单的网站 该网站有新闻 文章 视频剪辑 等 它的工作方式是在主页中我们向访问者展示一些课程 例如 gt math gt geography gt chemistry 用户在其中选择 1 网站内容会根据用户的选择
  • PHP 中的抽象类是什么?

    PHP 中的抽象类是什么 如何使用 抽象类是至少包含一个抽象方法的类 该方法中没有任何实际代码 只有名称和参数 并且已被标记为 抽象 这样做的目的是提供一种模板来继承并强制继承类实现抽象方法 因此 抽象类是介于常规类和纯接口之间的东西 此外
  • 如何缓存 twitter api 结果?

    我想缓存 twitter api 结果的结果并将其显示给用户 缓存结果的最佳方法是什么 我正在考虑根据时间限制将结果写入文件 可以吗 还是应该使用任何其他方法 最重要的是 理想的缓存时间是多少 我想显示来自 twitter 的最新内容 但
  • PHP:展平数组-最快的方法? [复制]

    这个问题在这里已经有答案了 是否有任何快速方法可以在不运行 foreach 循环的情况下展平数组并选择子键 在本例中为 键 和 值 或者 foreach 始终是最快的方法 Array 0 gt Array key gt string val
  • 如何使 WordPress 主题全宽?

    我尽了最大努力 但无法通过编辑 CSS 使以下主题全宽 屏幕 如果您能向我展示或给我有关此定制的提示 我将不胜感激 http demo mythemeshop com sociallyviral http demo mythemeshop
  • 如何使用xquery查找节点并向其添加子节点?

    是否可以使用xpath xquery查询特定的xml节点 然后向其导入 添加子节点 示例 代码取自http codepad org gJ1Y2LjM http codepad org gJ1Y2LjM 这是在类似的问题中提出的 但不相同 1
  • 使用 html5 分块上传文件

    我正在尝试使用 html5 的文件 API 分块上传文件 然后在服务器端用 php 重新组装它 我正在上传视频 但是当我在服务器端合并文件时 大小增加了 并且它变成了无效文件 请注意 以下 html5 代码仅适用于 chrome 浏览器 在
  • 如何在 PHP >= 5.3 严格模式下向对象添加属性而不产生错误

    这必须很简单 但我似乎找不到答案 我有一个通用的 stdClass 对象 foo没有属性 我想添加一个新属性 bar尚未定义 如果我这样做 foo new StdClass foo gt bar 1234 严格模式下的 PHP 会抱怨 将属
  • 有关于 PHP 中的 V8JS 的文档吗?

    有没有关于V8JS的文档 我是否只需要标准 PHP 或一些扩展即可使用 V8JS 我将非常感谢有关 PHP 中的 V8JS 的任何信息 要求 PHP 5 3 3 和 V8 库和标头安装在正确的路径中 Install http www php
  • SimpleXML插入处理指令(样式表)

    我想集成一个XSL文件在一个XML给我的字符串php CURL命令 我试过这个 output XML gived me by curl option hotel simplexml load string output hotel gt a
  • 在 PHP 命令行上显示完整的堆栈跟踪

    Problem 我的 PHP 堆栈跟踪缩写为 Stack trace 0 www html table app create php 128 SoapClient gt call call Array 1 www html table ap
  • MYSQL 按喜欢/不喜欢和受欢迎程度排序

    我有评论表 其中包括喜欢和不喜欢的内容 现在我在正确的顺序上遇到了问题 实际上 我的系统在顶部显示了最多点赞的评论 我正在 youtube 上寻找类似系统的东西 这意味着 100like 100dislikes 的评论的顺序高于 1 1 我

随机推荐