Woocommerce intermittently not sending extra emails – is my logic wrong?
-
I have two categories of product in a Woo store, and I have included a snippet in
functions.php
so that when an order is placed, two extra recipients also get a notification email. It is this last step that is ***sometimes*** not working – but the default email in Woocommerce always receives the the notification. Here is the code:// ADD EXTRA EMAIL RECIPIENTS add_filter( 'woocommerce_email_recipient_new_order', 'custom_email_recipient_new_order', 10, 2 ); function custom_email_recipient_new_order( $recipient, $order ) { // Not in backend when using $order (avoiding an error) if( ! is_a($order, 'WC_Order') ) return $recipient; // Define the email recipients / categories pairs in the array $recipients_categories = array( '[email protected]' => 'category1', '[email protected]' => 'category1', '[email protected]' => 'category2', ); // Loop through order items foreach ( $order->get_items() as $item ) { // Loop through defined product categories foreach ( $recipients_categories as $email => $category ) { if( has_term( $category, 'product_cat', $item->get_product_id() ) && strpos($recipient, $email) === false ) { $recipient .= ',' . $email; } } } return $recipient; }
So, if product is category 1, both recipient2 and recipient3 should get the email. If the product is category 2, we just trigger the email to recipient 3.
This seems to be working fine on the whole, but I’ve had an issue in the last 24 hours where 2 orders did not trigger the email to recipient2.
Emails are not in spam, and recipient 3 (and the default email) both received notifications.
I took this snippet from somewhere online, can’t remember where, and I just wonder if I’ve compromised the logic at all by messing with it.
- The topic ‘Woocommerce intermittently not sending extra emails – is my logic wrong?’ is closed to new replies.