1
votes

I am looking for a way to add all the details from the order to the email sent to admin and shop manager while hiding the shipping country from ONLY the customers' emails. is it possible to do so.

Currently I am using this snippet, but it removes all the customer details (billing and shipping) from both customers' email as well as from the emails received by the admin and shop manager.

function removing_customer_details_in_emails( $order, $plain_text, $email ){
    $wmail = WC()->mailer();
    remove_action( 'woocommerce_email_customer_details', array( $wmail, 'email_addresses' ), 20, 3 );
}
add_action( 'woocommerce_email_customer_details', 'removing_customer_details_in_emails', 5, 4 );
1

1 Answers

0
votes

You can do this by modifying the two templates email-addresses.php (plain text and html), you can find them in:

  • HTML /woocommerce/emails/email-addresses.php
  • Plain Text /woocommerce/emails/plain/email-addresses.php

In both templates you will need to add the following code just after the $shipping = $order->get_formatted_shipping_address(); line:

// only for emails sent to the customer
if ( $sent_to_admin == false ) {
    // returns the shipping address in raw, non-formatted way
    $raw_shipping_address = $order->get_address( 'shipping' );
    // removes the shipping country
    unset( $raw_shipping_address['country'] );
    // create the new updated shipping address
    $shipping = WC()->countries->get_formatted_address( $raw_shipping_address );
}

Modified templates will need to be added within your active child theme.

Remember to update your templates after future WooCommerce updates (if they are changed).

If you use the HTML template the new template will be added in:

  • /wp-content/themes/child-theme/woocommerce/emails/email-addresses.php

It should look like this:

<?php
/**
 * Email Addresses
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/emails/email-addresses.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see https://docs.woocommerce.com/document/template-structure/
 * @package WooCommerce\Templates\Emails
 * @version 3.9.0
 */

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

$text_align = is_rtl() ? 'right' : 'left';
$address    = $order->get_formatted_billing_address();
$shipping   = $order->get_formatted_shipping_address();

// only for emails sent to the customer
if ( $sent_to_admin == false ) {
    // returns the shipping address in raw, non-formatted way
    $raw_shipping_address = $order->get_address( 'shipping' );
    // removes the shipping country
    unset( $raw_shipping_address['country'] );
    // create the new updated shipping address
    $shipping = WC()->countries->get_formatted_address( $raw_shipping_address );
}

?><table id="addresses" cellspacing="0" cellpadding="0" style="width: 100%; vertical-align: top; margin-bottom: 40px; padding:0;" border="0">
    <tr>
        <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 if ( ! wc_ship_to_billing_address_only() && $order->needs_shipping_address() && $shipping ) : ?>
            <td style="text-align:<?php echo esc_attr( $text_align ); ?>; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif; padding:0;" valign="top" width="50%">
                <h2><?php esc_html_e( 'Shipping address', 'woocommerce' ); ?></h2>

                <address class="address"><?php echo wp_kses_post( $shipping ); ?></address>
            </td>
        <?php endif; ?>
    </tr>
</table>

If you use the Plain Text template the new template will be added in:

  • /wp-content/themes/child-theme/woocommerce/emails/plain/email-addresses.php

It should look like this:

<?php
/**
 * Email Addresses (plain)
 *
 * This template can be overridden by copying it to yourtheme/woocommerce/emails/plain/email-addresses.php.
 *
 * HOWEVER, on occasion WooCommerce will need to update template files and you
 * (the theme developer) will need to copy the new files to your theme to
 * maintain compatibility. We try to do this as little as possible, but it does
 * happen. When this occurs the version of the template file will be bumped and
 * the readme will list any important changes.
 *
 * @see     https://docs.woocommerce.com/document/template-structure/
 * @package WooCommerce\Templates\Emails\Plain
 * @version 3.4.0
 */

defined( 'ABSPATH' ) || exit;

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"; // WPCS: XSS ok.

if ( $order->get_billing_phone() ) {
    echo $order->get_billing_phone() . "\n"; // WPCS: XSS ok.
}

if ( $order->get_billing_email() ) {
    echo $order->get_billing_email() . "\n"; // WPCS: XSS ok.
}

if ( ! wc_ship_to_billing_address_only() && $order->needs_shipping_address() ) {
    $shipping = $order->get_formatted_shipping_address();

    // only for emails sent to the customer
    if ( $sent_to_admin == false ) {
        // returns the shipping address in raw, non-formatted way
        $raw_shipping_address = $order->get_address( 'shipping' );
        // removes the shipping country
        unset( $raw_shipping_address['country'] );
        // create the new updated shipping address
        $shipping = WC()->countries->get_formatted_address( $raw_shipping_address );
    }

    if ( $shipping ) {
        echo "\n" . esc_html( wc_strtoupper( esc_html__( 'Shipping address', 'woocommerce' ) ) ) . "\n\n";
        echo preg_replace( '#<br\s*/?>#i', "\n", $shipping ) . "\n"; // WPCS: XSS ok.
    }
}

The code has been tested and works.