0

Hello this is my first question here, so please bear with me. I have a Woocommerce(3.4.4) webshop and i would like to add a recipient to the New order email, if a product in the order has a specific variation attribute. Let say color is Dark Blue add email address 1 and when color is Light Green add email address 2.

I've found a function which a modified a little bit: Adding recipients to Woocommerce email notifications based on product variation term

But it just doesn't work. I cant find out why this script isn't doing what it supposed to do. Thanks in advance for the help!

add_filter( 'woocommerce_email_recipient_new_order', 'add_recipient', 10, 2 );
function add_recipient( $recipient, $order )
{
    if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;

    // Additional email recipient
    $additional_email1 = "email1@provider.nl";
    $additional_email2 = "email2@provider.nl";

    // The term slug
    $term_slug1 = "dark-blue";
    $term_slug2 = "light-green";

    $has_term = false;

    // Iterating through each order item
    foreach ($order->get_items() as $item_id => $item_obj) {
        $variation_id = $item_obj->get_variation_id();
        $variation_obj = wc_get_product($variation_id);
        $variation_attributes = $variation_obj->get_attributes();
        foreach( $variation_attributes as $taxonomy_key => $term_value ){

            if( $taxonomy_key == "pa_color" && $term_value == $term_slug1 ){
                $recipient .= ','. $additional_email1;
                $has_term = true;
                break; // stop searching
            }

            if( $taxonomy_key == "pa_color" && $term_value == $term_slug2 ){
                $recipient .= ','. $additional_email2;
                $has_term = true;
                break; // stop searching
            }
        }
        if( $has_term ) break; // stop the main loop
    }
    return $recipient;
}
  • you can try to debug by not using the `foreach` first and see if the mail is sent – Reigel Mar 26 '20 at 23:40
  • Hello Reigel. Thank you so much for your response. If i skip the conditions and just simply use the script below it works just fine. `code add_filter( 'woocommerce_email_recipient_new_order', 'add_recipient', 10, 2 ); function add_recipient( $recipient, $order ) { if ( ! is_a( $order, 'WC_Order' ) ) return $recipient; // Additional email recipient $additional_email1 = "email1@provider.nl"; $recipient .= ','. $additional_email1; return $recipient; }` – Xero023 Mar 27 '20 at 08:27

0 Answers0