The customer requested me to disable email notification for free products in WoocCmmerce, but only in case the order contains this free product id = 5274
If the order includes this free product and any other product, the order email notification should be trigger.
This is the code I use now:
add_filter('woocommerce_email_recipient_new_order', 'disable_notification_free_product', 10, 2);
function disable_notification_free_product($recipient, $order)
{
$page = $_GET['page'] = isset($_GET['page']) ? $_GET['page'] : '';
if ('wc-settings' === $page) {
return $recipient;
}
if (!$order instanceof WC_Order) {
return $recipient;
}
//the product id is 5274
$items = $order->get_items();
$items_cart = WC()->cart->get_cart_contents_count();
foreach ($items as $item) {
$product_id = $item['product_id'];
if ($product_id == 5274 and $items_cart == 1) {
$recipient = '';
}
return $recipient;
}
}
The code works before adding "and $items_cart == 1" to disable the email notification when the free product is in the order, but after adding the "and $items_cart == 1" nothing changed. Any advice?