首頁  >  問答  >  主體

從特定 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 天前411

全部回覆(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
  • 取消回覆