2
votes

For Woocommerce, I want to BCC woocommerce new order/processing order/on-hold order emails to additional multiple email addresses if specific products is purchased.

Current coding I have is able to send to specified emails if specific products purchased.

However, emails are send via 'To:' instead of 'BCC:' And there is 'Reply to' shown which I want it to be remove/hidden as well.

add_filter( 'woocommerce_email_recipient_new_order', 'conditional_recipient_new_email_notification', 15, 2 );

function conditional_recipient_new_email_notification( $recipient, $order ) {

    if( is_admin() ) return $recipient; // (Mandatory to avoid backend errors)

    // ## — YOUR SETTINGS (below) — ##

    $targeted_id = 1111; // HERE define your targeted product ID
    $addr_email = '[email protected], [email protected]'; // Here the additional recipient (If multiple, separate them by a coma)

    // Loop through orders items
    foreach ($order->get_items() as $item_id => $item ) {
        if ( $item->get_variation_id() == $targeted_id || $item->get_product_id() == $targeted_id ) {
            $recipient .= 'Bcc:' . ', ' . $addr_email . "\r\n";
            break; // Found and added – We stop the loop
        }
    }

   $targeted_id2 = 1821; // HERE define your targeted product ID
    $addr_email2 = '[email protected], [email protected]'; // Here the additional recipient (If multiple, separate them by a coma)

    // Loop through orders items
    foreach ($order->get_items() as $item_id => $item ) {
        if ( $item->get_variation_id() == $targeted_id2 || $item->get_product_id() == $targeted_id2 ) {
            $recipient .= 'Bcc: ' . ', ' . $addr_email2 . "\r\n";
            break; // Found and added – We stop the loop
        }
    }

    return $recipient;
}

How can I achieve the outcome I want?

I want BCC: instead of To: And Reply to: to be removed/hidden.

1

1 Answers

0
votes

I think you could do this by approaching it from the opposite direction: hooking woocommerce_email_headers where there's the opportunity to include BCC recipients, checking that it's running in the context of the emails you want to change (using the email ID parameter), and then using the logic you already have to run through the order items to decide whether to add a BCC header (which your existing recipient code should work for).

I haven't looked into the removing the reply-to header, but if you find it's included in the headers provided to your hook, you could search for it and replace it with an empty string.