繁体   English   中英

在某些 WooCommerce 电子邮件通知中删除帐单地址(仅显示送货地址)

[英]Remove billing address (only show shipping address) in certain WooCommerce email notifications

操作“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 ) ) );
}

对于 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 行

<td style="text-align:<?php echo esc_attr( $text_align ); ?>; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; border:0; padding:0;" valign="top" width="50%">
    <h2><?php esc_html_e( 'Billing address', 'woocommerce' ); ?></h2>

    <address class="address">
        <?php echo wp_kses_post( $address ? $address : esc_html__( 'N/A', 'woocommerce' ) ); ?>
        <?php if ( $order->get_billing_phone() ) : ?>
            <br/><?php echo wc_make_phone_clickable( $order->get_billing_phone() ); ?>
        <?php endif; ?>
        <?php if ( $order->get_billing_email() ) : ?>
            <br/><?php echo esc_html( $order->get_billing_email() ); ?>
        <?php endif; ?>
    </address>
</td>

<?php
// Targeting NOT a specific email. Multiple statuses can be added, separated by a comma
if ( ! in_array( $email_id, array( 'new_order' ) ) ) {
    ?>
    <td style="text-align:<?php echo esc_attr( $text_align ); ?>; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; border:0; padding:0;" valign="top" width="50%">
        <h2><?php esc_html_e( 'Billing address', 'woocommerce' ); ?></h2>

        <address class="address">
            <?php echo wp_kses_post( $address ? $address : esc_html__( 'N/A', 'woocommerce' ) ); ?>
            <?php if ( $order->get_billing_phone() ) : ?>
                <br/><?php echo wc_make_phone_clickable( $order->get_billing_phone() ); ?>
            <?php endif; ?>
            <?php if ( $order->get_billing_email() ) : ?>
                <br/><?php echo esc_html( $order->get_billing_email() ); ?>
            <?php endif; ?>
        </address>
    </td>
    <?php
}
?>


对于纯文本电子邮件:

步骤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( '#<br\s*/?>#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( '#<br\s*/?>#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
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM