php - 每种电子邮件类型的 Woocommerce 不同标题

标签 php wordpress email woocommerce hook

我使用 Woocommerce,我需要根据其类型更改电子邮件 header ,以便“customer-new-account.php”、“customer-processing-order.php”、“admin-new-order.php”(等等)......他们必须有不同的标题。

我刚刚在我的子模板中复制了 woocommerce“emails”文件夹,现在我需要知道如何更改代码。

感谢任何帮助。 ;-) 提前致谢。

最佳答案

我认为最简洁的方法是取消绑定(bind)默认的电子邮件 header 操作并制作您的自定义操作。如果您检查任何电子邮件模板,例如。 /woocommerce/templates/emails/admin-new-order.php ,您会在顶部看到他们已经将电子邮件对象作为第二个参数传递给操作,只是默认的 WC Hook 不会不要使用它:

 <?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 ?>

关于php - 每种电子邮件类型的 Woocommerce 不同标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27400044/

相关文章:

ruby-on-rails - Sendgrid 上的 Heroku 错误

php mysql 查询资源 null 错误

php - Symfony 3 登录后重定向到 http 而不是 https

php - 如何在指定时间后自动从数据库中删除数据?

php - 我应该在用户输入错误时抛出异常吗?

javascript - 在响应式网格站点中拥有全屏宽度背景

php - 复选框按钮在 Wordpress 中不起作用

CSS 倾斜的子菜单项显示不正确

java - 使用 PHP 的大型邮件发送平台

email - 在本地主机 smtp 上发送邮件不起作用