简体   繁体   English

如何从发送给 WooCommerce 客户的电子邮件中删除发货国家/地区?

[英]How to remove shipping country from emails sent to WooCommerce customers?

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.我正在寻找一种将订单中的所有详细信息添加到发送给管理员和商店经理的 email 的方法,同时仅对客户的电子邮件隐藏发货国家/地区。 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.目前我正在使用此代码段,但它从客户的 email 以及管理员和商店经理收到的电子邮件中删除了所有客户详细信息(计费和运输)。

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

You can do this by modifying the two templates email-addresses.php (plain text and html) , you can find them in:您可以通过修改两个模板email-addresses.php (plain text and html)来做到这一点,您可以在以下位置找到它们:

  • HTML /woocommerce/emails/email-addresses.php HTML /woocommerce/emails/email-addresses.php
  • Plain Text /woocommerce/emails/plain/email-addresses.php纯文本/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();在这两个模板中,您都需要在$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).请记住在未来的 WooCommerce 更新后更新您的模板(如果它们已更改)。

If you use the HTML template the new template will be added in:如果您使用HTML 模板,则新模板将添加到:

  • /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.该代码已经过测试并且可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM