基于WooCommerce电子邮件通知中产品类别的不同收件人

时间:2019-01-04 07:38:13

标签: php wordpress woocommerce custom-taxonomy email-notifications

我正在为一家同时销售虚拟产品(费用和短途旅行费)和实物(制服)的学校建立一个站点,但是他们希望每个类别的订单通知都将被发送给单独的收件人由不同的部门。

例如,所有统一类别的订单都将发送给收件人一个,而其他所有类别的订单将发送给收件人两个...我遇到了多种发送基于产品的自定义电子邮件消息的方法,但是这些都不满足我的要求

我也希望能够不使用Woocommerce Advanced Notifications之类的插件来完成此操作。

在此方面的任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:2)

以下内容将根据您的产品类别将其他收件人添加到“新订单”电子邮件通知中,您将在带有电子邮件收件人/产品类别对的索引数组中定义该邮件:

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;
}

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

  

注释:

     
      
  • 已定义的产品类别可以是术语ID,术语塞子或术语名称。
  •   
  • 每个产品类别都需要在相关产品中定义,因为has_term()WordPress条件函数无法处理父项。
  •   

用于处理父产品类别的附加信息:

// Custom conditional function that checks for categories (including parent)
function has_product_categories( $product_id, $categories ) {
     // Initializing
    $parent_term_ids = $categories_ids = array();
    $taxonomy        = 'product_cat';

    // Convert categories term names and slugs to categories term ids
    foreach ( $categories as $category ){
        if( is_numeric( $category ) ) {
            $categories_ids[] = (int) $category;
        } elseif ( term_exists( sanitize_title( $category ), $taxonomy ) ) {
            $categories_ids[] = get_term_by( 'slug', sanitize_title( $category ), $taxonomy )->term_id;
        }
    }

    // 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;
}

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

  

注意:产品类别可以是术语ID,术语条或术语名称。


类似:Different recipients based on products sold in WooCommerce email notification

相关问题