【发布时间】:2021-07-02 07:11:33
【问题描述】:
我正在尝试根据订单自定义字段“交付类型”更改我的电子邮件标题,以帮助商店的员工确定订单是用于交付还是收集,他们希望电子邮件标题采用颜色编码。
这里有几个有用的帖子解释了如何取消绑定 WooCommerce 电子邮件标题,然后有效地在每个电子邮件模板(新订单、处理等)中手动添加对 email-header.php 模板的调用或使用 switch 语句根据电子邮件类型应用新标头。
我正在尝试根据一些自定义订单元数据自定义 email-header.php 模板,用于新订单电子邮件通知。
目前我正在 admin-new-order.php 模板中执行此操作,但由于您必须全局取消设置/取消绑定标头,您必须为每个邮件类型/模板手动添加对 email-header.php 模板的调用.
基于Woocommerce different headers for each email types 答案代码,这是我的代码尝试:
add_action( 'init', 'replace_email_header_hook' );
function replace_email_header_hook(){
remove_action( 'woocommerce_email_header', array( WC()->mailer(), 'email_header' ) );
add_action( 'woocommerce_email_header', 'woocommerce_email_header', 10, 2 );
}
function woocommerce_email_header( $email_heading, $email ) {
$order = $email->object;
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
$del_type = get_post_meta( $order_id, 'delivery_type', true );
switch($email->id) {
case 'new_order':
if ($del_type == 'delivery') {
$template = 'emails/email-header-alt.php';
}
else if ($del_type == 'pickup') {
$template = 'emails/email-header.php';
}
break;
default:
$template = 'emails/email-header.php';
}
wc_get_template( $template, array( 'email_heading' => $email_heading ) );
}
当尝试从该钩子中的 Order 对象获取 $order_id 变量时,该问题似乎与该变量有关,我不确定这是否可行。
【问题讨论】:
标签: php email templates woocommerce email-notifications