Exclude billing address from specific WooCommerce email notifications, show only shipping address
P粉071626364
P粉071626364 2023-12-29 14:46:55
0
1
378

The "woocommerce_email_customer_details" action contains billing address and shipping address data. All I need is the shipping address.

How can I achieve this goal? Below is my current"New Order"email plain text template (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

reply all (1)
P粉806834059

For HTML email:

If you use this code, it will remove billing and shipping information from the required email notifications. This is because it ensures that the related template files are not loaded:

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 );

However, since you only want to partially hide/not show the output of the related template file (/emails/email-addresses.php)a different approach will be required强>.

This can be done byadapting the/emails/email-addresses.phptemplate file to your needs.This template can be overridden by copying ityourtheme/woocommerce/emails/email-addresses.php

Note:Since you only want to apply this to specific email notifications, you will need to use$email->id. Since this is not passed to the relevant template file by default, aworkaroundis required.

How to pass "$email" to the WooCommerce email template file if it is not available by defaultThis is described in Reply Codes.


So to answer your question:

Step 1)Addin thefunctions.php

file of the active child theme (or active theme)
// 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 );

Step 2)In the/emails/email-addresses.phptemplate file, @version 5.6.0

Replace lines 18 - 20

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

and

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'] : '';

and

Replace lines 28 - 40

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

and

           

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


For plain text email:

Step 1)Addin thefunctions.php

file of the active child theme (or active theme)
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 );

Step 2)In the/emails/plain/email-addresses.phptemplate file, @version 5.6.0

Replace lines 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 }

and

// 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 } }
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!