登录  /  注册
从特定 WooCommerce 电子邮件通知中排除帐单地址,仅显示送货地址
P粉071626364
P粉071626364 2023-12-29 14:46:55
[PHP讨论组]

“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粉071626364
P粉071626364

全部回复(1)
P粉806834059

对于 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 } }
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 //m.sbmmt.com/ All Rights Reserved | php.cn | 湘ICP备2023035733号