首页 > 解决方案 > 如果购买了特定产品,Woocommerce BCC 向多封电子邮件发送电子邮件通知

问题描述

对于 Woocommerce,如果购买了特定产品,我想将 woocommerce 新订单/处理订单/暂停订单电子邮件密件发送到其他多个电子邮件地址。

如果购买了特定产品,我拥有的当前编码能够发送到指定的电子邮件。

但是,电子邮件是通过“收件人:”而不是“密件抄送:”发送的,并且显示了“回复”,我希望它也被删除/隐藏。

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 = 1111; // HERE define your targeted product ID
    $addr_email = 'additional1@gmail.com, additional2@gmail.com'; // Here the additional recipient (If multiple, separate them by a coma)

    // 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 .= 'Bcc:' . ', ' . $addr_email . "\r\n";
            break; // Found and added – We stop the loop
        }
    }

   $targeted_id2 = 1821; // HERE define your targeted product ID
    $addr_email2 = 'additional3@gmail.com, additional4@gmail.com'; // Here the additional recipient (If multiple, separate them by a coma)

    // Loop through orders items
    foreach ($order->get_items() as $item_id => $item ) {
        if ( $item->get_variation_id() == $targeted_id2 || $item->get_product_id() == $targeted_id2 ) {
            $recipient .= 'Bcc: ' . ', ' . $addr_email2 . "\r\n";
            break; // Found and added – We stop the loop
        }
    }

    return $recipient;
}

我怎样才能达到我想要的结果?

我想要 BCC: 而不是 To: And Reply to: 被删除/隐藏。

标签: phpwordpresswoocommerce

解决方案


我认为您可以通过从相反的方向接近它来做到这一点:挂钩 woocommerce_email_headers 有机会包含密件抄送收件人的地方,检查它是否在您要更改的电子邮件的上下文中运行(使用电子邮件 ID 参数),然后使用您已经必须运行订单项目以决定是否添加密件抄送标头(您现有的收件人代码应该适用于该标头)的逻辑。

我没有考虑删除回复标题,但如果您发现它包含在提供给您的钩子的标题中,您可以搜索它并将其替换为空字符串。


推荐阅读