我认为最简洁的方法是取消绑定默认的电子邮件标题操作并进行自定义操作。如果您检查任何电子邮件模板,例如。
/woocommerce/templates/emails/admin-new-order.php ,您将在顶部看到他们已经将电子邮件对象作为第二个参数传递给操作,只是默认的 WC 挂钩不使用它:
<?php do_action( 'woocommerce_email_header', $email_heading, $email ); ?>
所以在你的functions.php 中你可以这样做:
// replace default WC header action with a custom one
add_action( 'init', 'ml_replace_email_header_hook' );
function ml_replace_email_header_hook(){
remove_action( 'woocommerce_email_header', array( WC()->mailer(), 'email_header' ) );
add_action( 'woocommerce_email_header', 'ml_woocommerce_email_header', 10, 2 );
}
// new function that will switch template based on email type
function ml_woocommerce_email_header( $email_heading, $email ) {
// var_dump($email); die; // see what variables you have, $email->id contains type
switch($email->id) {
case 'new_order':
$template = 'emails/email-header-new-order.php';
break;
default:
$template = 'emails/email-header.php';
}
wc_get_template( $template, array( 'email_heading' => $email_heading ) );
}
如果你不需要切换整个文件,而只是想对现有标题进行一些小改动,你可以将电子邮件类型参数传递到模板中,只需将底部模板包含替换为:
wc_get_template( $template, array( 'email_heading' => $email_heading, 'email_id' => $email->id ) );
然后在您的标题模板中将其用作$email_id,例如:
<?php if($email_id == 'new_order'): ?>
<h2>Your custom subheader to appear on New Order notifications only</h2>
<?php endif ?>