如果订单包含WooCommerce中某个类别的商品,则向CC发送新订单电子邮件

时间:2020-09-01 07:21:54

标签: php wordpress woocommerce email-notifications carbon-copy

如果订单包含来自父产品类别的商品,我想向cc发送管理员新订单电子邮件:

我正在使用下面的代码,但这似乎不起作用。邮件正在发送,但我收到未定义变量的通知。 CC中的人也未收到消息

add_filter( 'woocommerce_email_headers', 'bbloomer_order_completed_email_add_cc_bcc', 9999, 3 );
 
function bbloomer_order_completed_email_add_cc_bcc( $headers, $email_id, $order ) {
    
      $order = wc_get_order( $order_id ); // The WC_Order object
    if ( 'new_order' == $email_id && $orderid['product_cat']== 69) {
        $headers .= "Cc: Name <your@email.com>" . "\r\n"; // del if not needed
    
    }
    return $headers;
}

有人想仔细看看吗?

1 个答案:

答案 0 :(得分:1)

  • 没有必要使用wc_get_order(),您已经可以通过参数访问$order对象
  • $orderid['product_cat']不存在
  • 通过代码中的注释进行解释

因此您可以使用

function filter_woocommerce_email_headers( $header, $email_id, $order ) {
    // New order
    if ( $email_id == 'new_order' ) {       
        // Set categories
        $categories = array( 'categorie-1', 'categorie-2' );
        
        // Flag = false by default
        $flag = false;
        
         // Loop trough items
        foreach ($order->get_items() as $item ) {
            // Product id
            $product_id = $item['product_id'];

            // Has term (a certain category in this case)
            if ( has_term( $categories, 'product_cat', $product_id ) ) {
                // Found, flag = true, break loop
                $flag = true;
                break;
            }
        }
        
        // True
        if ( $flag ) {  
            // Prepare the the data
            $formatted_email = utf8_decode('My test <mytestemail@test.com>');

            // Add Cc to headers
            $header .= 'Cc: ' . $formatted_email . '\r\n';
        }
    }

    return $header;
}
add_filter( 'woocommerce_email_headers', 'filter_woocommerce_email_headers', 10, 3 );