2

Basically, in woocommerce you have the option to input multiple email addresses (separated by commas) of who to send the completed order to, in WooCommerce -> Settings -> Emails -> New order. But I need a way to send to only one of these recipients based on the zip code of the customer who is ordering the product. Or completely overwrite woocommerce's way of handling this.

How can I tie into the function responsible for this, in order to send to the correct recipient? Basically, is there a hook defined for this, or does a plugin exist for something like this, or will I have to edit core WooCommerce files? If edits are needed to core files, can someone point me in the right direction as to which files will need edits?

Solomon Closson
  • 6,111
  • 14
  • 73
  • 115

2 Answers2

2

I had a bit of trouble with helgatheviking's answer above and also a slightly different use case. My problems/needs were:

  • I didn't quite understand the filter name as presented above.
  • The public function get_recipient() inside of class-wc-email.php expected a string but was getting an array.
  • A also wanted to conditionally add the extra recipient based on the payment method, rather than postcode.

Here's what I did instead:

  • Added full filter name, not just the suffix: woocommerce_email_recipient_new_order
  • Replaced explode() and array_push() with string concatenation $email .= ',' . $additional_email;.
  • Conditionally checked payment method: if( $order->get_payment_method() == "cod" ).

Full example:

add_filter( 'woocommerce_email_recipient_new_order' , 'so_26429482_add_recipient', 20, 2 );
function so_26429482_add_recipient( $email, $order ) {

    // !empty($order) prevents a fatal error in WooCommerce Settings
    // !empty($email) prevents the duplicate email from being sent in cases where the filter is run outside of when emails are normally sent. In my case, when using https://wordpress.org/plugins/woo-preview-emails/
    if(!empty($order) && !empty($email)) {
    
        $additional_email = "isf@domain.co";
    
        if( $order->get_payment_method() == "cod" ) {
            $email .= ',' . $additional_email;
        } else {
            $email .= ',another@domain.co';
        }
    
    }
    return $email;
}
montrealist
  • 5,593
  • 12
  • 46
  • 68
1

Each email has a filter that allows you to adjust the recipients of that email. The filter name is essentially woocommerce_email_recipient_{$email_id}.

So the following would filter the "to" addresses for the "new_order" email.

add_filter( 'new_order' , 'so_26429482_add_recipient', 20, 2 );
function so_26429482_add_recipient( $email, $order ) {
    $additional_email = "somebody@somewhere.net";
    if( $order->shipping_postcode == "90210" ){
        $email = explode( ',', $email );
        array_push( $email, $additional_email );
    }
    return $email;
}

I'm not 100% certain on the conditional logic, but I think that should check the shipping zip code and subsequently send to the additional email address.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
helgatheviking
  • 25,596
  • 11
  • 95
  • 152
  • Where do you get the email id from? So as to use that number? – Solomon Closson Oct 17 '14 at 22:27
  • All the email classes have an id which can be seen in the `includes/emails` folder. – helgatheviking Oct 19 '14 at 10:04
  • @helgatheviking, great answer! I was hoping you might have a suggestion for a question I posted here: http://stackoverflow.com/questions/41216160/woocommerce-add-recipient-to-order-confirmation-email that would allow me to add a new recipient to the Admin Confirmation Email based on an option the customer has selected.. thank you for any help you're able to provide. – revive Dec 19 '16 at 16:34