如果在 Woocommerce 中购买了特定产品,则会向特定地址发送电子邮件通知

cod*_*uru 3 wordpress orders email-notifications woocommerce hook-woocommerce

我在我的 WordPress 网站中使用 woocommerce 插件。我想知道如果客户购买了产品 A,如何向特定电子邮件地址发送电子邮件通知。

在 Woocommerce 中购买特定产品时如何向特定地址发送电子邮件通知?

Loi*_*tec 5

当在订单项中找到特定定义的产品 ID 时,以下代码将向新电子邮件通知添加自定义电子邮件收件人:

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;
}
Run Code Online (Sandbox Code Playgroud)

代码位于活动子主题(或活动主题)的 function.php 文件中。经过测试并有效。