WooCommerce: Adding the Shipping Address to New Order Emails
Problem: You're using WooCommerce and want to include the customer's shipping address in the new order email notifications. However, the default email template only displays the billing address.
Solution: This article will guide you through the simple steps of adding the shipping address to your WooCommerce new order emails.
Scenario: You're running an online store using WooCommerce and want to ensure your customers have all the necessary information in their order confirmation emails. Currently, the email only displays the billing address, leading to potential confusion if the customer has opted for a different shipping address.
Original Code:
The default WooCommerce new order email template is generated using a PHP template file called emails/customer-new-order.php
. This file contains the following code snippet that displays the billing address:
<?php if ( $order->get_billing_address() ) : ?>
<p><?php echo esc_html( $order->get_formatted_billing_address() ); ?></p>
<?php endif; ?>
Adding the Shipping Address:
To include the shipping address in your new order emails, you'll need to modify this template file by adding a similar code snippet for the shipping address. Follow these steps:
-
Locate the Template File: Navigate to your WordPress installation directory, then to
wp-content/plugins/woocommerce/templates/emails/
and find thecustomer-new-order.php
file. -
Add the Shipping Address Code: Below the existing billing address code, paste the following:
<?php if ( $order->get_shipping_address() ) : ?> <p><?php echo esc_html( $order->get_formatted_shipping_address() ); ?></p> <?php endif; ?>
-
Save Changes: Save the changes to your
customer-new-order.php
file.
Additional Considerations:
-
Customizing the Display: You can customize the way the shipping address is displayed by modifying the code. For example, you can add a title like "Shipping Address:" before the address.
-
Conditional Logic: You can use conditional logic to only display the shipping address if it differs from the billing address. This can prevent redundancy and maintain a cleaner email format.
-
Plugin Alternatives: Several WooCommerce plugins can automate this process, offering additional customization options and features. Consider searching for plugins like "WooCommerce Email Address" or "WooCommerce Shipping Address" for more advanced solutions.
Example:
After implementing the code above, your new order email might look like this:
Subject: Your Order is Confirmed!
Body:
Thank you for your order!
Order Number: #12345
Billing Address:
123 Main Street Anytown, CA 12345
Shipping Address:
456 Oak Avenue Springfield, IL 62701
Conclusion:
By adding a few lines of code, you can easily include the shipping address in your WooCommerce new order emails. This will ensure your customers have all the information they need, leading to a more seamless and transparent ordering process.
Resources: