“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 ) ) );
}
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
对于 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 行
与
对于纯文本电子邮件:
步骤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 行
与
// 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 } }