0

The action 'woocommerce_email_customer_details' includes both the billing and shipping address data. I only need the shipping address.

How can I achieve this? 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 ) ) );
}
aynber
  • 22,380
  • 8
  • 50
  • 63
Jisoros
  • 1
  • 2

1 Answers1

0

For HTML emails:

If you would use this code, it would remove both the billing and shipping information from the desired email notification. This is because it ensures that the relevant template file is 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 from the relevant template file (/emails/email-addresses.php) a different approach will be needed.

This can be done by adapting the /emails/email-addresses.php template file to your needs. The template can be overridden by copying it to yourtheme/woocommerce/emails/email-addresses.php

Note: since you only want to apply this for a specific email notification, you will need to use $email->id. Since this is not passed on to the relevant template file by default, a workaround will be necessary.

This is described in How to pass "$email" to WooCommerce emails template files if not available by default answer code.


So to answer your question:

Step 1) Add in functions.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 /emails/email-addresses.php template file, @version 5.6.0

Replace line 18 - 20

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

With

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 line 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>

With

<?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
}
?>


For plain text emails:

Step 1) Add in functions.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 /emails/plain/email-addresses.php template file, @version 5.6.0

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

With

// 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
    }
}
7uc1f3r
  • 28,449
  • 17
  • 32
  • 50
  • Thank you for your swift and thoughtful reply. I used the "Shortcode Actions Filters" plugin in place of functions.php. I inserted your plain text email code and viola! It worked. However, it removed the shipping data and displayed the billing data. I wanted the inverse. So, a took a stab in the dark and replaced 'woocommerce' with 'Shipping address', 'woocommerce' and get_formatted_billing_address() ) with get_formatted_shipping_address() ) and seems to have worked. Hopefully I haven't broken something else. – Jisoros May 15 '22 at 04:44
  • @Jisoros If you want to do the reverse, just replace `if ( ! in_array(` with `if ( in_array(` – 7uc1f3r May 15 '22 at 06:22