In a recent Business Bloomer Club Slack thread, a WooCommerce user asked about customizing the shipping address displayed in the order emails.
Specifically, they wanted to modify the address shown in the woocommerce_email_customer_details
template. Here’s a guide on how to approach this customization.
Solution: Use the woocommerce_order_get_formatted_shipping_address
Filter
The woocommerce_order_get_formatted_shipping_address
filter allows you to modify the formatted shipping address for WooCommerce order emails. This filter can be applied to adjust the address output without altering WooCommerce’s core files.
- Add the Filter in
functions.php
:
- Add this snippet to your theme’s
functions.php
file or in a custom plugin to change the shipping address format.
add_filter( 'woocommerce_order_get_formatted_shipping_address', 'custom_shipping_address_format', 10, 3 );
function custom_shipping_address_format( $address, $raw_address, $order ) {
// Customize the $address array as needed
$address = $address ? $address : 'Custom Shipping Address Here';
return $address;
}
- Customize the
$address
Array:
- Modify elements of
$address
to suit your requirements. For example, you can add notes, change the format, or replace specific fields based on your store’s needs.
- Test the Email Output:
- Complete a test order to verify that the customized shipping address displays correctly in order emails.
Alternative: Overriding email-addresses.php
If you need more granular control over the email layout, you can override the email-addresses.php
template.
- Copy the Template:
- Copy
woocommerce/templates/emails/email-addresses.php
to your theme’s WooCommerce folder atyour-theme/woocommerce/emails/email-addresses.php
.
- Customize the Address Output:
- Within the copied template, modify the structure or add conditional statements to customize how the shipping address displays.
Conclusion
Using the woocommerce_order_get_formatted_shipping_address
filter provides a quick way to customize the shipping address in WooCommerce emails, while overriding email-addresses.php
offers more control over the entire email layout. Either approach enables you to tailor WooCommerce order emails to meet your specific requirements.