WooCommerce 电子邮件通知中基于产品类别的不同收件人 [英] Different recipients based on product category in WooCommerce email notification

查看:28
本文介绍了WooCommerce 电子邮件通知中基于产品类别的不同收件人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一所销售虚拟产品(费用和远足费用)和实体产品(制服)的学校建立一个网站,但是他们希望在处理每个类别时将订单通知发送给不同的收件人由不同部门.

I am setting up a site for a school that sells both virtual products (fees and excursion payments) and Physical (uniforms) however they would like to have order notifications for each category to be sent to separate recipients as they are dealt with by different departments.

例如,所有统一类别的订单都转到收件人一,而所有其他类别的订单都转到收件人二...我遇到过多种根据产品发送自定义电子邮件的方法,但是这些方法都不符合我的要求.

For example all uniform category orders are to go to recipient one, while all the others go to recipient two... I have come across multiple ways to send custom emails messages based on the product, however none of these meet my requirements.

我还希望能够在不使用 Woocommerce Advanced Notifications 等插件的情况下做到这一点.

I would also like to be able to do this without the use of a plugin like Woocommerce Advanced Notifications.

对此的任何帮助将不胜感激.

Any assistance with this would be greatly appreciated.

推荐答案

以下将向新订单"添加其他收件人基于您的产品类别的电子邮件通知,您将在带有电子邮件收件人/产品类别对的索引数组中定义:

The following will add additional recipients to "New Order" email notification based on your product categories, that you will define in an indexed array with an email recipient / product category pairs:

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 / categories pairs in the array
    $recipients_categories = array(
        'email.one@email.com'   => 'category-one',
        'email.two@email.com'   => 'category-two',
        'email.three@email.com' => 'category-three',
    );

    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        // Loop through defined product categories
        foreach ( $recipients_categories as $email => $category ) {
            if( has_term( $category, 'product_cat', $item->get_product_id() ) && strpos($recipient, $email) === false ) {
                $recipient .= ',' . $email;
            }
        }
    }
    return $recipient;
}

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

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

注意事项:

  • 定义的产品类别可以是术语 ID、术语 slug 或术语名称.
  • 需要在相关产品中定义每个产品类别,因为 has_term() WordPress 条件函数不处理父术语.


###Addition 处理父产品类别:


###Addition to handle parent product categories:

// Custom conditional function that handle parent product categories too
function has_product_categories( $categories, $product_id = 0 ) {
    $parent_term_ids = $categories_ids = array(); // Initializing
    $taxonomy        = 'product_cat';
    $product_id      = $product_id == 0 ? get_the_id() : $product_id;

    if( is_string( $categories ) ) {
        $categories = (array) $categories; // Convert string to array
    }

    // Convert categories term names and slugs to categories term ids
    foreach ( $categories as $category ){
        $result = (array) term_exists( $category, $taxonomy );
        if ( ! empty( $result ) ) {
            $categories_ids[] = reset($result);
        }
    }

    // Loop through the current product category terms to get only parent main category term
    foreach( get_the_terms( $product_id, $taxonomy ) as $term ){
        if( $term->parent > 0 ){
            $parent_term_ids[] = $term->parent; // Set the parent product category
            $parent_term_ids[] = $term->term_id; // (and the child)
        } else {
            $parent_term_ids[] = $term->term_id; // It is the Main category term and we set it.
        }
    }
    return array_intersect( $categories_ids, array_unique($parent_term_ids) ) ? true : false;
}

// Adding custom recipients based on product categories
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 / categories pairs in the array
    $recipients_categories = array(
        'email.one@email.com'   => 'category-one',
        'email.two@email.com'   => 'category-two',
        'email.three@email.com' => 'category-three',
    );

    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        // Loop through defined product categories
        foreach ( $recipients_categories as $email => $category ) {
            if( has_product_categories( $item->get_product_id(), array( $category ) ) && strpos($recipient, $email) === false ) {
                $recipient .= ',' . $email;
            }
        }
    }
    return $recipient;
}

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

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

注意:产品类别可以是术语 ID、术语 slug 或术语名称.

Note: The Product categories can be term Ids, term slugs or term names.


类似:基于 WooCommerce 电子邮件通知中销售的产品的不同收件人

这篇关于WooCommerce 电子邮件通知中基于产品类别的不同收件人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆