1

I am trying to achieve the following thing: Woocommerce additional recipient to new order email based on product variation attribute

However, I am also trying to achieve this for when the product is a simple product, but does contain the attribute.

Still, I cannot figure out how to do this for simple products...

Here is my code so far:

    add_filter( 'woocommerce_email_recipient_new_order', 'add_recipient', 10, 2 );
function add_recipient( $recipient, $order )
{
// Additional email recipient
$additional_email1 = "random@gmail.com";
$additional_email2 = "ranndom2@gmail.com";

// The term slug
$term_slug1 = "d1";
$term_slug2 = "d2";

$has_term = false; // Initializing

// Loop though order items
foreach ( $order->get_items() as $item ){
    if( $item->get_product_id() > 0 ){
        $product = $item->get_product(); // The WC_Product Object
        $product_id = $item->get_product_id(); // Product ID

        // Loop through product attributes set in the variation
        foreach( $product->get_attributes() as $taxonomy => $term_slug ){
            // comparing attribute parameter value with current attribute value
            if ( $term_slug === $term_slug1 ) {
                $has_term = true;
                $recipient .= ','. $additional_email1;
            }

            if ( $term_slug === $term_slug2 ) {
                $has_term = true;
                $recipient .= ','. $additional_email2;
            }
        }
    }

    if( $has_term ) break; // stop the main loop

}

return $recipient;
}

However, the code does not return any error whatsoever so I am not sure what I am doing wrong. Can anyone give me a hint please?

Apex C
  • 13
  • 3
  • Do the same terms contain simple products? Please add screenshots of how you set up both simple and variation products. – Bhautik Dec 10 '21 at 07:26
  • Hello Bhautik, thank you so much for your help! I am actually no longer using variable products as they are not compatible with our inventory system. So we will only use simple products. The simple products contain an attribute, named Distrib., which contains two terms: d1 and d2. All products will only be from either d1 or d2. If the product contains the term d1, then the email should go to $additional_email1 . If the product contains the term d2, then the email should go to $additional_email2. I cannot figure out how to filter for attributes in normal products – Apex C Dec 10 '21 at 09:55

1 Answers1

0

I revised your code. try the below code.

function add_recipient( $recipient, $order ){

    if( $order ) {

        // Additional email recipient
        $additional_email1 = "random@gmail.com";
        $additional_email2 = "ranndom2@gmail.com";

        // The term slug
        $term_slug1 = "d1";
        $term_slug2 = "d2";

        $has_term = false; // Initializing

        // Loop though order items
        foreach ( $order->get_items() as $item ){

            if( $item->get_product_id() > 0 ){

                $product    = $item->get_product(); // The WC_Product Object
                $product_id = $item->get_product_id(); // Product ID

                if( !empty( $product->get_attributes() ) ){

                    $product_attributes = $product->get_attributes();

                    // Loop through product attributes set in the variation
                    foreach( $product_attributes as $taxonomy => $term_slug ){

                        $options = $product_attributes[$taxonomy]['options'];

                        if( !empty( $options ) ){

                            foreach ( $options as $key => $option ) {
                        
                                $term = get_term_by( 'ID', $option, $taxonomy );
                                
                                if( $term && $term->slug == $term_slug1 ){
                                    $has_term   = true;
                                    $recipient .= ','. $additional_email1;
                                }
                                
                                if( $term && $term->slug == $term_slug2 ){
                                    $has_term   = true;
                                    $recipient .= ','. $additional_email2;
                                }                       

                            }

                        }

                    }

                }

            }

            if( $has_term ) break; // stop the main loop

        }

    }
    
}
add_filter( 'woocommerce_email_recipient_new_order', 'add_recipient', 10, 2 );
Bhautik
  • 10,143
  • 2
  • 13
  • 36
  • Hello Bhautik, many thanks again. Unfortunately, the code is not sending out any email. Any idea why? – Apex C Dec 14 '21 at 12:15
  • I have tested this code and working fine for me. – Bhautik Dec 14 '21 at 12:25
  • I see now. I am getting a critical error for some reason: `Fatal error: Uncaught Error: Call to a member function get_items() on null in ... functions.php on line 67` Line 67 is... `foreach ( $order->get_items() as $item ){` – Apex C Dec 17 '21 at 12:49