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

耗尽温柔 提交于 2020-01-05 02:16:06

问题


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?


回答1:


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.




回答2:


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('name@domain.com', 'Product #' . $product_id . ' has been purchased', $custom_massage);
            return true;
        }
    }
}



回答3:


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.



来源:https://stackoverflow.com/questions/51577659/email-notification-to-a-particular-address-if-a-specific-product-is-purchased-in

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!