Alpeshinfo
Alpeshinfo

Reputation: 48

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?

Upvotes: 3

Views: 5301

Answers (3)

WPZA
WPZA

Reputation: 931

You should be able to use the official WooCommerce Advanced Notifications extension to generate an advanced notification to send an email to a specific person.

Upvotes: 0

LoicTheAztec
LoicTheAztec

Reputation: 254492

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  = '[email protected]'; // 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.

Upvotes: 6

Shir Gans
Shir Gans

Reputation: 2027

add_action('woocommerce_thankyou', 'send_product_purchase_notification');
function send_product_purchase_notification($orderId)
{
    $order = new WC_Order($orderId);
    foreach ($order->get_items() as $item) {
        $product_id = (is_object($item)) ? $item->get_product_id() : $item['product_id'];
        if ($product_id == 1234) {
            $custom_massage = 'Custom email content';
            wp_mail('[email protected]', 'Product #' . $product_id . ' has been purchased', $custom_massage);
            return true;
        }
    }
}

Upvotes: 1

Related Questions