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


Woocommerce different headers for each email types

我使用Woocommerce,我需要根据其类型更改电子邮件标题,以便"客户-新帐户.php","客户处理订单.php","管理员-新订单.php"(等等)...它们必须具有不同的标头。

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

任何帮助是感激的。提前谢谢。

我相信

最干净的方法是取消绑定默认的电子邮件标题操作并创建自定义操作。如果您检查任何电子邮件模板,例如。 /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 ?>

正如评论中提到的,我认为没有办法在woocommerce_email_header钩上使用条件逻辑。你可以使用$header变量,但它是一个长字符串,它可能会改变。

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

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它可能如下所示:

<?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' ) ) : ?>

我的本地设置没有电子邮件,所以我已经用以下内容对其进行了测试:

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模板正在调用新标头。

我可以在电子邮件头.php文件上使用did_action函数使用条件标头,以检查触发了哪种类型的电子邮件。

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

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;
};

我在插件 https://wordpress.org/plugins/wc-multiple-email-recipients/中做了类似的事情。

使用 woocommerce_email_headers 时,可以将IDHeader作为参数传递。

add_filter( 'woocommerce_email_headers', 'change_my_header_based_on_mail_type', 10, 2);
// Pass ID and header as arguments which is in the scope of the filter. 
// The ID allows us to identify the mail. Header allows us to overwrite the header. 
function change_my_header_based_on_mail_type( $headers = '', $id = '') 
{       
  // WooCommerce core. If the ID for notification is "New Order"
  if ($id == 'new_order') 
   {
    //append the following to the header
    $headers .= 'My custom output ' . "'r'n";
    //break;
   }
  // If the ID for notification is "Cancelled Order"
  if ($id == 'cancelled_order') 
   {
    //append the following to the header
    $headers .= 'My custom output ' . "'r'n";
    //break;
return $headers;
}