Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
650 views
in Technique[技术] by (71.8m points)

php - Hide shipping address on local pickup in WooCommerce email notifications

How do you hide the "Shipping Address" info section in the customers' processing email if "Local Pickup" is chosen?

I don't know how to provide code for this.

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Yes it's possible (see the update at the bottom of this page)

overriding WooCommerce templates (copying the WooCommerce plugin templates folder to your active child theme or theme, renaming it woocommerce).
For that please be sure to read the related docs First.

Once above correctly done, you have to edit 2 templates located in that woocommerce folder inside your active child theme or theme. the files are located in:

  1. emails (sub folder) > email-addresses.php (php file).

    Replace the code of this template from line 19:
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

?><table id="addresses" cellspacing="0" cellpadding="0" style="width: 100%; vertical-align: top;" border="0">
    <tr>
        <td class="td" style="text-align:left; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;" valign="top" width="50%">
            <h3><?php _e( 'Billing address', 'woocommerce' ); ?></h3>

            <p class="text"><?php echo $order->get_formatted_billing_address(); ?></p>
        </td>
        <?php // ======================> Here begins the customization

            $shipping_local_pickup = false;
            if ( $items_totals = $order->get_order_item_totals() ) {
                foreach ( $items_totals as $items_total ) {
                    if ( $items_total['value'] == 'Local Pickup' && !$shipping_local_pickup ) $shipping_local_pickup = true;
                }
            }
            // End

            if ( ! wc_ship_to_billing_address_only() && $order->needs_shipping_address() && ( $shipping = $order->get_formatted_shipping_address() ) && !$shipping_local_pickup ) : ?>
            <td class="td" style="text-align:left; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;" valign="top" width="50%">
                <h3><?php _e( 'Shipping address', 'woocommerce' ); ?></h3>

                <p class="text"><?php echo $shipping; ?></p>
            </td>
        <?php endif; ?>
    </tr>
</table>
  1. emails (sub folder) > plain (sub folder) > email-addresses.php (php file).

    Replace the code of this template from line 19:
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

echo "
" . strtoupper( __( 'Billing address', 'woocommerce' ) ) . "

";
echo preg_replace( '#<brs*/?>#i', "
", $order->get_formatted_billing_address() ) . "
";

 // ======================> Here begins the customization

$shipping_local_pickup = false;
if ( $items_totals = $order->get_order_item_totals() ) {
    foreach ( $items_totals as $items_total ) {
        if ( $items_total['value'] == 'Local Pickup' && !$shipping_local_pickup ) $shipping_local_pickup = true;
    }
} // End

if ( ! wc_ship_to_billing_address_only() && $order->needs_shipping_address() && ( $shipping = $order->get_formatted_shipping_address() ) && !$shipping_local_pickup ) {
    echo "
" . strtoupper( __( 'Shipping address', 'woocommerce' ) ) . "

";
    echo preg_replace( '#<brs*/?>#i', "
", $shipping ) . "
";
}

I have jus added this to detect when Local Pickup is used in an order:

$shipping_local_pickup = false;
if ( $items_totals = $order->get_order_item_totals() ) {
    foreach ( $items_totals as $items_total ) {
        if ( $items_total['value'] == 'Local Pickup' && !$shipping_local_pickup ) $shipping_local_pickup = true;
    }
}

And this in the existing if statement to stop the display of shipping address (for Local Pickup):

&& !$shipping_local_pickup

Update: Is possible to use a much more compact code using the ** WC_Abstract_Order class - has_shipping_method()** method this way:

$shipping_local_pickup = false;
if ( $order->has_shipping_method( 'local_pickup' ) )
    $shipping_local_pickup = true;

And this in the existing if statement to stop the display of shipping address (for Local Pickup):

&& !$shipping_local_pickup

References:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...