Change sender name and email address for specific WooCommerce email notifications
Asked Answered
I

1

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.

Interruption answered 8/9, 2017 at 13:7 Comment(1)
The email object is passed to the woocommerce_email_from_address 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.Furthermore
S
13

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 = '[email protected]';

    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 )

Spinthariscope answered 8/9, 2017 at 23:51 Comment(7)
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?Interruption
@Interruption 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.Spinthariscope
tnx once again.Interruption
@Spinthariscope 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?Od
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?Try
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 youPerrin
I have seen your new question. Reply to only allow one email address, this is logical, think about it.Spinthariscope

© 2022 - 2024 — McMap. All rights reserved.