php - 基于 WooCommerce 电子邮件通知中销售的产品的不同收件人

标签 php wordpress woocommerce product email-notifications

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

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

我可以在 functions.php 中添加一些代码来实现这一点吗?

最佳答案

基于 "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 文件。经过测试并有效。

关于php - 基于 WooCommerce 电子邮件通知中销售的产品的不同收件人,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55546448/

相关文章:

javascript - Ajax 验证没有请求发送到服务器

mysql - SQL : Update column (username) with content from another column in same table (email address)

php - Woocommerce 返回价格未正确显示

php - 在 Woocommerce 中更改产品库存可用性文本

php - 更改自定义产品类型的 WooCommerce "In Stock"文本(预购)

php - mysql中的多维表

javascript - 使用 C3JS 的 x 轴上的多个值(时间序列)

PHP数据库查询

wordpress - 在 Microsoft Azure 上的 Wordpress 上上传主题

wordpress - 为什么我的站点地图生成后没有任何链接页面?