我想弄清楚我在这里想念什么...... 我制作了一个插件来创建自定义的 woocommerce 电子邮件类型,包括必要的模板和所有内容。客户希望通过向订单操作元框添加操作来发送它。
所以我注册了新的电子邮件类型:
class Custom_WC_Email {
public function __construct() {
// Filtering the emails and adding our own email.
add_filter( 'woocommerce_email_classes', array( $this, 'register_email' ), 90, 1 );
// Absolute path to the plugin folder.
define( 'CUSTOM_WC_EMAIL_PATH', plugin_dir_path( __FILE__ ) );
}
/**
* @param array $emails
*
* @return array
*/
public function register_email( $emails ) {
require_once 'emails/class-wc-send-pickup-information-email.php';
$emails['WC_Information_Pickup_Email'] = new WC_Information_Pickup_Email();
return $emails;
}
}
new Custom_WC_Email();
接下来我添加了一个像这样的元框操作:
function add_order_meta_box_action( $actions ) {
// add "mark printed" custom action
$actions['wc_custom_order_action'] = __( 'Mail informatie verzending/afhalen', 'my-textdomain' );
return $actions;
}
add_action( 'woocommerce_order_actions', 'add_order_meta_box_action' );
function process_order_meta_box_action( $order ) {
$message = sprintf( __( 'Mail met informatie over verzending/afhalen verzonden', 'my-textdomain' ), wp_get_current_user()->display_name );
$order->add_order_note( $message );
// add the flag
update_post_meta( $order->id, '_wc_order_mail_information_pickup_sent', 'yes' );
}
add_action( 'woocommerce_order_action_wc_custom_order_action', 'process_order_meta_box_action' );
并扩展类并包含包含的模板文件:
类 WC_Information_Pickup_Email 扩展了 WC_Email {
/**
* Set email defaults
*
* @since 0.1
*/
public function __construct() {
$base = plugin_dir_path( __FILE__ );
// set ID, this simply needs to be a unique name
$this->id = 'wc_send_pickup_class';
$this->customer_email = true;
// this is the title in WooCommerce Email settings
$this->title = __( 'Verzending/ophalen informatie', 'your-plugin-textdomain' );
// this is the description in WooCommerce email settings
$this->description = __( 'Informatie over uw verzending of uw afhaallokatie', 'your-plugin-textdomain' );
// these are the default heading and subject lines that can be overridden using the settings
$this->heading = __( 'Verzending/ophalen informatie', 'your-plugin-textdomain' );
$this->subject = __( 'Meer informatie over uw bestelling', 'your-plugin-textdomain' );
// 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 = 'templates/information-pickup-order.php';
$this->template_plain = 'templates/plain/information-pickup-order.php';
$this->template_base = ''.$base.'';
$this->placeholders = array(
'{order_date}' => '',
'{order_number}' => '',
);
// Manually send via admin order actions.
add_action( 'woocommerce_order_action_wc_custom_order_action', array( $this, 'trigger' ) );
// Call parent constructor to load any other defaults not explicity defined here
parent::__construct();
}
最后但并非最不重要的是触发发送电子邮件:
public function trigger( $order_id, $order = false ) {
$this->setup_locale();
if ( $order_id && ! is_a( $order, 'WC_Order' ) ) {
$order = wc_get_order( $order_id );
}
if ( is_a( $order, 'WC_Order' ) ) {
$this->object = $order;
$this->recipient = $this->object->get_billing_email();
$this->placeholders['{order_date}'] = wc_format_datetime( $this->object->get_date_created() );
$this->placeholders['{order_number}'] = $this->object->get_order_number();
}
if ( $this->is_enabled() && $this->get_recipient() ) {
$success = $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() );
}
$this->restore_locale();
}
/**
* Get content html.
*
* @return string
*/
public function get_content_html() {
return wc_get_template_html(
$this->template_html,
array(
'order' => $this->object,
'email_heading' => $this->get_heading(),
'additional_content' => $this->get_additional_content(),
'sent_to_admin' => false,
'plain_text' => false,
'email' => $this
), '', $this->template_base );
}
抱歉问了这么长的问题,但我尝试包含所有相关内容...
关于为什么发送双倍的任何想法?