【发布时间】:2015-07-07 14:05:36
【问题描述】:
跟进此线程:Woocommerce Refund Email
我有一个似乎无法解决的问题,我阅读了所有关于使用带有自定义触发器的自定义类扩展 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;
});
对此有什么想法或解决方法吗?
可能值得一提的是,我正在使用 Peddlar 主题的子主题,但这不应该吗?我正在使用 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 类功能。
唯一不工作的,如果我将电子邮件类型设置为普通,Content-Type 仍然设置为:text/html 而不是text/plain,导致我所有的换行符在我的模板中消失。在默认的 WooCommerce 电子邮件通知中,这确实可以正常工作。
我的猜测是$mailer->send 调用中的$My_Class->get_headers() 参数不能正常工作。
有什么解决办法吗?
我可以在模板本身中手动设置 Content-Type 吗?
谢谢
【问题讨论】:
-
更新了问题的解决方法
-
您应该使用
add_action( 'woocommerce_customer_save_address_notification', array( $this, 'trigger' ) );而不是add_action( 'woocommerce_customer_save_address', array( $this, 'trigger' ) );看到_notification 部分了吗?这是必需的。
标签: php wordpress email woocommerce