【问题标题】:Different recipients based on products sold in WooCommerce email notification基于 WooCommerce 电子邮件通知中销售的产品的不同收件人
【发布时间】:2019-08-28 00:14:53
【问题描述】:

我希望在 WooCommerce 上为不同的企业销售一些虚拟物品。因此,当客户结帐时,我希望将电子邮件发送给相关企业(而不是管理员)。

因此,当产品 A 售出时,一封电子邮件将发送至 a@email.com。产品 B 售出后,电子邮件将发送至 b@email.com。产品 C 售出后,电子邮件将发送至 c@email.com...等等。

是否有一些代码可以添加到functions.php 来实现这一点?

【问题讨论】:

    标签: php wordpress woocommerce product email-notifications


    【解决方案1】:

    基于Different recipients based on product category in WooCommerce email notification 答案代码,以下将允许根据产品 ID 添加不同的电子邮件收件人:

    add_filter( 'woocommerce_email_recipient_new_order', 'custom_email_recipient_new_order', 10, 2 );
    function custom_email_recipient_new_order( $recipient, $order ) {
        // Not in backend when using $order (avoiding an error)
        if( ! is_a($order, 'WC_Order') ) return $recipient;
    
        // Define the email recipients / product Ids pairs
        $recipients_product_ids = array(
            'product.one@email.com'   => array(37),
            'product.two@email.com'   => array(40),
            'product.three@email.com' => array(53, 57),
        );
    
        // Loop through order items
        foreach ( $order->get_items() as $item ) {
            // Loop through defined product categories
            foreach ( $recipients_product_ids as $email => $product_ids ) {
                $product_id   = $item->get_product_id();
                $variation_id = $item->get_variation_id();
                if( array_intersect([$product_id, $variation_id], $product_ids) && strpos($recipient, $email) === false ) {
                    $recipient .= ',' . $email;
                }
            }
        }
        return $recipient;
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且可以工作。

    【讨论】:

      猜你喜欢
      • 2019-05-30
      • 2017-06-15
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-26
      相关资源
      最近更新 更多