
How to replace Woocommerce emails addresses section with custom data
Today is another seemingly simple task that nevertheless could not be solved with either Stackoverflow or Chat-GPT :)We need replace the customer shipping address in the email template for WooCommerce order notifications for some shipping method.
The Stackoverflow solution
add_action( 'woocommerce_email_customer_details', 'sent_to_admin_custom_email_addresses', 5, 4 );
function sent_to_admin_custom_email_addresses( $order, $sent_to_admin, $plain_text, $email ){
if( in_array( $email->id, array( 'new_order', 'cancelled_order', 'failed_order' ) ) ) {
$mailer = WC()->mailer();
remove_action( 'woocommerce_email_customer_details', array( $mailer, 'email_addresses' ), 20 );
add_action( 'woocommerce_email_customer_details', 'custom_email_addresses', 20, 1 );
}
}
function custom_email_addresses( $order ) {
if ( is_a( $order, 'WC_Order' ) ) :
$text_align = is_rtl() ? 'right' : 'left';
$address = $order->get_formatted_billing_address();
?><table id="addresses" cellspacing="0" cellpadding="0" style="width: 100%; vertical-align: top; margin-bottom: 40px; padding:0;" border="0">
...