Search code examples
wordpresswoocommercehook-woocommerceordersemail-notifications

Email notification to a particular address if a specific product is purchased in Woocommerce


I am using the woocommerce plugin in my Wordpress website. I am wondering how can I send an email notification to a specific address email if product A is purchased by customer.

How to send an email notification to a specific address when a specific product is purchased in Woocommerce?


Solution

  • The code below will add a custom defined email recipient to New Email Notification When a specific defined product Id is found in order items:

    add_filter( 'woocommerce_email_recipient_new_order', 'conditional_recipient_new_email_notification', 15, 2 );
    function conditional_recipient_new_email_notification( $recipient, $order ) {
        if( is_admin() ) return $recipient; // (Mandatory to avoid backend errors)
    
        ## --- YOUR SETTINGS (below) --- ##
    
        $targeted_id = 37; // HERE define your targeted product ID
        $addr_email  = 'name@domain.com'; // Here the additional recipient
    
        // Loop through orders items
        foreach ($order->get_items() as $item_id => $item ) {
            if( $item->get_variation_id() == $targeted_id || $item->get_product_id() == $targeted_id ){
                $recipient .= ', ' . $addr_email; 
                break; // Found and added - We stop the loop
            }
        }
    
        return $recipient;
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.