6

How to change email sender address and name in WooCommerce for specific email notifications?

For example:
Change the sender name and email address just for customer processing order email notifications.

But not for all email notifications, just for specific ones.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Nikola
  • 163
  • 1
  • 3
  • 10
  • The email object is passed to the [`woocommerce_email_from_address`](https://github.com/woocommerce/woocommerce/blob/master/includes/emails/class-wc-email.php#L531) filter as the second variable. That object will have an `id` property that you can use to conditionally change the from address for specific emails. – helgatheviking Sep 08 '17 at 17:02

1 Answers1

12

The sender name and email address are set here (at the end of Woocommerce "Emails" setting tab:

enter image description here

This fields are passed through dedicated filters hook that allow you to change conditionally the values.

Here is an example conditionally restricted to "customer processing email notification":

// Change sender name
add_filter( 'woocommerce_email_from_name', function( $from_name, $wc_email ){
    if( $wc_email->id == 'customer_processing_order' ) 
        $from_name = 'Jack the Ripper';

    return $from_name;
}, 10, 2 );

// Change sender adress
add_filter( 'woocommerce_email_from_address', function( $from_email, $wc_email ){
    if( $wc_email->id == 'customer_processing_order' )
        $from_email = 'jack.the.ripper@freek.com';

    return $from_email;
}, 10, 2 );

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works.

Some other WC_Email Ids that you can use in your condition: - 'customer_completed_order'
- 'customer_on_hold_order'
- 'customer_refunded_order'
- 'customer_new_account'
- 'new_order' ( admin notification )
- 'cancelled_order' ( admin notification )
- 'failed_order' ( admin notification )

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thank you very much, that solved the problem, but can it be done in OOP ? when i try it like this: In the constructor: `add_filter( 'woocommerce_email_from_address', [ $this, 'email_sender_addressess' ] );` then the class function `public function email_sender_addressess( $from_email, $wc_email) { ... }`. The error appears that says that the second parameter is not passed. Any ideas? – Nikola Sep 11 '17 at 13:49
  • @Nikola Try this instead: `add_filter( 'woocommerce_email_from_address', array( $this, 'email_sender_addressess' ), 10, 2 );` where `10` is the priority and `2` the number of parameters in the function … This normally will solve this issue. same thing for the other hook. – LoicTheAztec Sep 11 '17 at 14:42
  • tnx once again. – Nikola Sep 11 '17 at 14:47
  • @LoicTheAztec thanks for this snippet. I am creating a custom plugin in that I just want to send email through ajax. But my problem is how can I check id for it as I am using my custom functions. Can I set an id as well? – NewUser Sep 05 '18 at 10:55
  • 1
    My god, you save lives! The only question I have is: If your answer wouldn't exist, where on earth this is written in the docs of woocommerce & would someone be able to find it? – Igor Bykov Apr 23 '19 at 01:40
  • I apologize for being bold. Is it possible to add two sender addresses? I mean, the email address where customer responses to WooCommerce order notifications are sent? Would this be possible @LoicTheAztec? Thank you – unity-student Aug 04 '23 at 11:19
  • 1
    I have seen your new question. Reply to only allow one email address, this is logical, think about it. – LoicTheAztec Aug 04 '23 at 11:30