4

I'm looking to filter the email headers of the new order form in woocommerce. I'm trying to replace the customer email address with the main site admin email address. We need to do this because Gmail is flagging new orders as spam because the from address is not the same as the reply to address. The function below works partially:

add_filter( 'woocommerce_email_headers', 'woo_add_reply_to_wc_admin_new_order', 10, 3 );

function woo_add_reply_to_wc_admin_new_order( $headers = '', $id = '', $order ) {
if ( $id == 'new_order' ) {
    $reply_to_email = get_option( 'admin_email', '' );
    $headers .= "Reply-to: <$reply_to_email>\r\n";
}
return $headers;}

This function is adding the site admin email address but doesn't remove the customer email.

Anyone out there have any ideas on how to remove the customer email from the reply to field? Note: We still need to have a record of the customer email on the order - we just don't want it in the email headers

Any help would be great!

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Marco
  • 293
  • 3
  • 13

1 Answers1

6

The correct way to make it work is:

add_filter( 'woocommerce_email_headers', 'new_order_reply_to_admin_header', 20, 3 );
function new_order_reply_to_admin_header( $header, $email_id, $order ) {

    if ( $email_id === 'new_order' ){
        $email = new WC_Email($email_id);
        $header = "Content-Type: " . $email->get_content_type() . "\r\n";
        $header .= 'Reply-to: ' . __('Administrator') . ' <' . get_option( 'admin_email' ) . ">\r\n";
    }
    return $header;
}

This code goes on function.php file of your active child theme (or theme). Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Many thanks Loic. I'll test this now. Can the function reside in a MU plugin? – Marco Mar 08 '18 at 12:19
  • @Marco is better to test it first in function.php file of your active theme, as it's tested and working this way… For custom code in Woocommerce, is better to have a child theme with all related code in it's own function.php file… Or to build a simple custom plugin with all woocommerce related code customizations. Is never good to enable Woocommerce customizations code in a third party plugin like in MU plugins. **Remember that Woocommerce is a huge and very sensible plugin.** – LoicTheAztec Mar 08 '18 at 12:36
  • @LoicTheAztec This code works wonderfully to adjust the reply-to address to the admin email for the WordPress install. Is there a way to hard-code an email address into this? Right now our 'new order' emails are defaulting to the customer in the reply-to and we want them to go to a specific email address every time. – aronmoshe_m Jun 13 '18 at 15:48
  • @ludditedev I have answer your question. Not sure that I have understood it. – LoicTheAztec Jun 13 '18 at 16:46
  • If you want to use this for all WC emails, do not forget to wrap your code to `if( $email ) {}` (the 4th parameter, missing in Loic's answer), because the `woocommerce_email_headers` is called for mails (low stock, no stock, backorder), in which the `$email` variable is actually null (and `$email_id` is empty as well), resulting in PHP fatal errors and (in my experience) creation of duplicate WC orders (if 5 low stock notifications are sent upon order creation, 5 duplicate orders are created). – Kristián Filo Jan 31 '23 at 07:07