编程语言
首页 > 编程语言> > php – 基于WooCommerce电子邮件通知中的产品类别的不同收件人

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

作者:互联网

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

例如,所有统一类别订单都将转到收件人1,而所有其他订单转到收件人2 …我遇到了多种方式根据产品发送自定义电子邮件消息,但这些都不符合我的要求.

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

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

解决方法:

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

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文件中.经过测试和工作.

Notes:

  • The defined Product categories can be term Ids, term slugs or term names.
  • Each product category need to be defined in the related products as has_term() WordPress conditional function doesn’t handle parent terms.

除了处理父产品类别外:

// 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文件中.经过测试和工作.

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

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

标签:php,wordpress,woocommerce,email-notifications,custom-taxonomy
来源: https://codeday.me/bug/20191002/1841604.html