Search code examples
phpwordpresswoocommercehook-woocommerceemail-notifications

How to send all Woocommerce emails to an additional email address?


I am looking to send all emails to another email in order to parse them (using Sendgrid inbound parse).

This is what I currently have:

function custom_add_email_address_to_emails( $headers, $object ) {
    $additional_email = '[email protected]';
    $headers .= 'Cc: ' . $additional_email . "\r\n";
    return $headers;
}

add_filter( 'woocommerce_email_headers', 'custom_add_email_address_to_emails', 10, 2 );

CC-ing, unfortunately, doesn’t seem to work for inbound parse, so I need to actually add the email as a recipient. Do you know how I might do this?


Solution

  • You can add recipients to WooCommerce pre-defined email notifications using the composite hook woocommerce_email_recipient_{$email_id}.

    In the code below:

    1. You will define the desired email notifications in the first function (See this linked answer for other email IDs),
    2. You will define the desired additional recipient (emails) in the 2nd function.

    The code:

    add_action('woocommerce_init', 'additional_email_recipient_for_notifications');
    function additional_email_recipient_for_notifications(){
        // HERE below define the desired email notification
        $email_ids = array(
            'new_order',
            'customer_on_hold_order', 
            'customer_processing_order',
            'customer_completed_order',
            'customer_refunded_order',
            'customer_partially_refunded_order',
            'cancelled_order',
            'failed_order',
        );
    
        foreach( $email_ids as $email_id ) {
            add_filter( "woocommerce_email_recipient_{$email_id}",  "additional_email_recipient" );
        }
    }
    
    function additional_email_recipient( $recipient ) {
        if ( is_admin() && isset($_GET['tab']) && $_GET['tab'] === 'email' ) 
            return $recipient;
    
        // HERE below define your additional email recipients
        $additional_recipient = array('[email protected]', '[email protected]');
    
        $recipient .= ',' . implode(',', $additional_recipient);
    
        return $recipient;
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). Tested and works.

    Related: Target a specific email notification with the email id in WooCommerce