首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Woocommerce对每种电子邮件类型都有不同的标题

Woocommerce对每种电子邮件类型都有不同的标题
EN

Stack Overflow用户
提问于 2014-12-10 11:36:29
回答 4查看 18.6K关注 0票数 4

我使用Woocommerce,我需要根据电子邮件的类型更改电子邮件头,以便“Custome-new-Account.php”、“order.php”、"admin-new-order.php“(等等).他们肯定有不同的标题。

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

任何帮助都是感激的。-)预先谢谢。

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-12-10 16:58:52

正如注释中提到的,我认为在woocommerce_email_header钩子上没有使用条件逻辑的方法。您可以使用$header变量,但是它是一个长字符串,它可能会发生变化。

首先,我们删除现有的电子邮件标题:

代码语言:javascript
运行
复制
function so_27400044_remove_email_header(){
    remove_action( 'woocommerce_email_header', array( WC()->mailer(), 'email_header' ) );
}
add_action( 'init', 'so_27400044_remove_email_header' );

然后直接调用电子邮件模板中的特定标头模板。例如,在customer-invoice.php模板中,我们可以调用wc_get_template()直接加载适当/特定的标头。假设您复制了email-header.php模板,并将客户发票的名称重命名为email-header-invoice.php,则可能如下所示:

代码语言:javascript
运行
复制
<?php
/**
 * Customer invoice email
 *
 * @author      WooThemes
 * @package     WooCommerce/Templates/Emails
 * @version     2.2.0
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit; // Exit if accessed directly
}

?>

<?php do_action( 'woocommerce_email_header', $email_heading ); ?>

<?php wc_get_template( 'emails/email-header-invoice.php', array( 'email_heading' => $email_heading ) ) ; ?>

<?php if ( $order->has_status( 'pending' ) ) : ?>

我的本地设置不发送电子邮件,所以我用以下方法进行了测试:

代码语言:javascript
运行
复制
function kia_testing(){ 

$order= wc_get_order( 381 );
        ob_start();
        wc_get_template( 'emails/customer-processing-order.php', array(
            'order'         => $order,
            'email_heading' => 'some title',
            'sent_to_admin' => false,
            'plain_text'    => true
        ) );
        echo ob_get_clean();

}
add_action( 'woocommerce_before_single_product' , 'kia_testing' );

我看到修改后的customer-processing-order.php模板正在调用新的标头。

票数 2
EN

Stack Overflow用户

发布于 2016-09-20 01:20:02

我认为最干净的方法是解除默认的电子邮件标题操作,并使您的自定义一个。如果你检查任何电子邮件模板,例如。/woocommerce/templates/emails/admin-new-order.php,您将在顶部看到它们已经将电子邮件对象作为第二个参数传递给操作,只有默认的WC挂钩不使用它:

代码语言:javascript
运行
复制
 <?php do_action( 'woocommerce_email_header', $email_heading, $email ); ?>

因此,在您的functions.php中,您可以这样做:

代码语言:javascript
运行
复制
// 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 ) );
}

如果您不需要切换整个文件,只需要对现有的标头进行小的更改,您可以将电子邮件类型参数传递到模板中,只需将底部的模板包含替换为:

代码语言:javascript
运行
复制
wc_get_template( $template, array( 'email_heading' => $email_heading, 'email_id' => $email->id ) );

然后在标题模板中使用它作为$email_id,例如:

代码语言:javascript
运行
复制
<?php if($email_id == 'new_order'): ?>
    <h2>Your custom subheader to appear on New Order notifications only</h2>
<?php endif ?>
票数 9
EN

Stack Overflow用户

发布于 2015-10-27 03:05:04

我可以使用条件头使用did_action函数的email-header.php文件,以检查哪种类型的电子邮件被触发。

您可以检查其类构造函数上的每封电子邮件的操作。每封电子邮件在woocommerce/includes/ email /文件夹(即woocommerce/includes/emails/class-wc-email-customer-note.php )上都有自己的类文件。在检查未由add_action触发的发票电子邮件和新用户电子邮件时,可以检查$order$user_login变量:

代码语言:javascript
运行
复制
if ( 0 < did_action('woocommerce_order_status_pending_to_processing_notification') || 0 >= did_action('woocommerce_order_status_pending_to_on-hold_notification') ) {
    // your specific code for this case here;
}
else if ( 0 < did_action('woocommerce_new_customer_note_notification') ) {
    // your specific code for this case here;
}
else if ( $order ) {
    echo '<p>aajast_woo_mail 7 =   invoice + customer-invoice; </p>';
}
else if ( $user_login ) {
    // your specific code for this case here;
};
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27400044

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档