首页  >  问答  >  正文

从特定 WooCommerce 电子邮件通知中排除帐单地址,仅显示送货地址

“woocommerce_email_customer_details”操作包含帐单地址和送货地址数据。我只需要送货地址。

我怎样才能实现这个目标?以下是我当前的“新订单”电子邮件纯文本模板 (admin-new-order.php)

/*Admin new order email (plain text)*/
defined( 'ABSPATH' ) || exit;
do_action( 'woocommerce_email_customer_details', $order, $sent_to_admin, $plain_text, $email );
do_action( 'woocommerce_email_order_details', $order, $sent_to_admin, $plain_text, $email );
if ( $additional_content ) {
    echo esc_html( wp_strip_all_tags( wptexturize( $additional_content ) ) );
}

P粉071626364P粉071626364269 天前413

全部回复(1)我来回复

  • P粉806834059

    P粉8068340592023-12-30 10:19:53

    对于 HTML 电子邮件:

    如果您使用此代码,它会从所需的电子邮件通知中删除帐单和送货信息。这是因为它确保相关的模板文件不被加载:

    function action_woocommerce_email_customer_details( $order, $sent_to_admin, $plain_text, $email ) {
        $mailer = WC()->mailer();
        remove_action( 'woocommerce_email_customer_details', array( $mailer, 'email_addresses' ), 20 );
    }
    add_action( 'woocommerce_email_customer_details', 'action_woocommerce_email_customer_details', 5, 4 );
    

    但是,由于您只想部分隐藏/不显示相关模板文件 (/emails/email-addresses.php) 的输出将需要一种不同的方法.

    这可以通过根据您的需要调整/emails/email-addresses.php模板文件来完成。 可以通过将模板复制到来覆盖该模板 yourtheme/woocommerce/emails/email-addresses.php

    注意:由于您只想将此应用于特定电子邮件通知,因此您需要使用 $email->id。由于默认情况下这不会传递到相关模板文件,因此需要解决方法

    如果默认情况下不可用,如何将“$email”传递到 WooCommerce 电子邮件模板文件应答码中对此进行了描述.


    所以回答你的问题:

    步骤1)在活动子主题(或活动主题)的functions.php文件中添加

    // Header - set global variable
    function action_woocommerce_email_header( $email_heading, $email ) {
        $GLOBALS['email_id'] = $email->id;
    } 
    add_action( 'woocommerce_email_header', 'action_woocommerce_email_header', 10, 2 );
    

    步骤 2)/emails/email-addresses.php 模板文件中,@version 5.6.0

    替换第 18 - 20 行

    if ( ! defined( 'ABSPATH' ) ) {
        exit;
    }
    

    if ( ! defined( 'ABSPATH' ) ) {
        exit;
    }
    
    // Getting the email ID global variable
    $ref_name_globals_var = isset( $GLOBALS ) ? $GLOBALS : '';
    $email_id = isset( $ref_name_globals_var['email_id'] ) ? $ref_name_globals_var['email_id'] : '';
    

    替换第 28 - 40 行

    
        

    get_billing_phone() ) : ?>
    get_billing_phone() ); ?> get_billing_email() ) : ?>
    get_billing_email() ); ?>

    
        
            

    get_billing_phone() ) : ?>
    get_billing_phone() ); ?> get_billing_email() ) : ?>
    get_billing_email() ); ?>


    对于纯文本电子邮件:

    步骤1)在活动子主题(或活动主题)的functions.php文件中添加

    function action_woocommerce_email_order_details( $order, $sent_to_admin, $plain_text, $email ) {
        // Plain text is TRUE and target specific email notification
        if ( $plain_text && $email->id == 'new_order' ) {
            $GLOBALS['email_id'] = $email->id;
        }
    }
    add_action( 'woocommerce_email_order_details', 'action_woocommerce_email_order_details', 1, 4 );
    

    步骤 2)/emails/plain/email-addresses.php 模板文件中,@version 5.6.0

    替换第 20 - 29 行

    echo "\n" . esc_html( wc_strtoupper( esc_html__( 'Billing address', 'woocommerce' ) ) ) . "\n\n";
    echo preg_replace( '##i', "\n", $order->get_formatted_billing_address() ) . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
    
    if ( $order->get_billing_phone() ) {
        echo $order->get_billing_phone() . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
    }
    
    if ( $order->get_billing_email() ) {
        echo $order->get_billing_email() . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
    }
    

    // Getting the email ID global variable
    $ref_name_globals_var = isset( $GLOBALS ) ? $GLOBALS : '';
    $email_id = isset( $ref_name_globals_var['email_id'] ) ? $ref_name_globals_var['email_id'] : '';
    
    // Targeting NOT a specific email. Multiple statuses can be added, separated by a comma
    if ( ! in_array( $email_id, array( 'new_order' ) ) ) {
        echo "\n" . esc_html( wc_strtoupper( esc_html__( 'Billing address', 'woocommerce' ) ) ) . "\n\n";
        echo preg_replace( '##i', "\n", $order->get_formatted_billing_address() ) . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
    
        if ( $order->get_billing_phone() ) {
            echo $order->get_billing_phone() . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
        }
    
        if ( $order->get_billing_email() ) {
            echo $order->get_billing_email() . "\n"; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
        }
    }
    

    回复
    0
  • 取消回复