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?