troubleshooting Question

Woocommerce: How to dynamically replace default new order email notification sender FROM address with customer's email address??

Avatar of worthyking1
worthyking1Flag for United States of America asked on
PHPWordPress* woo commerce
7 Comments1 Solution66 ViewsLast Modified:
I am trying to modify the Woo Commerce new_order (admin) email notification function to change the default FROM address to the customer's name & email address.  You would think this would be simple and yet I have wasted many hours to no avail.

Essentially, all I need to do is hook into the woocommerce_email_from_address filter and replace the $from_address value with the customer's email address, however I cannot figure out how to access the $order object during this hook in order to get the $order->get_billing_email() value and assign it to $from_address in the filter.

I have tried many things, including an action hook on woocommerce_new_order, getting the customer details there and then saving them to a global variable (also tried setting them into a cookie), but still when my woocommerce_email_from_address filter fires the $from_address returned value is always blank.

This is the current (non-working) version of what I have in my child theme functions.php file:

add_action( 'woocommerce_new_order', 'get_the_customer_details', 10, 1 );

function get_the_customer_details( $order_id ) {
    
     // Get an instance of the WC_Order object
     $order = wc_get_order( $order_id );

     global $rhpname, $rhpcustomeremail;

    $rhpname = $order->get_billing_first_name().' '.$order->get_billing_last_name();
    $rhpcustomeremail = $order->get_billing_email();
    if(empty($rhpcustomeremail))    { $rhpcustomeremail = 'support@mydomain.com'; }
    	
    setcookie("rhpname", $rhpname, time() + (360), "/"); // 86400 = 1 day
    setcookie("rhpcustomeremail", $rhpcustomeremail, time() + (360), "/"); // 86400 = 1 day

}

// Change sender name
add_filter( 'woocommerce_email_from_name', function( $from_name, $wc_email ){
    if( $wc_email->id == 'new_order' ) 
    {
        $cookie_name = "rhpname";
        if(!isset($_COOKIE[$cookie_name])) {
            $from_name = $_COOKIE[$cookie_name];
        } else {
            $from_name = 'RHP WooCommerce Store';
        }
        
    }
    return $from_name;
}, 11, 2 );

// Change sender adress
add_filter( 'woocommerce_email_from_address', function( $from_email, $wc_email ){
    if( $wc_email->id == 'new_order' )
    {
        $cookie_name = "rhpcustomeremail";
        if(!isset($_COOKIE[$cookie_name])) {
            $from_email = $_COOKIE[$cookie_name];
        } else {
            $from_email = 'support@mydomain.com';
        }
        
    }   
    return $from_email;
}, 11, 2 );
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 7 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros