WooCommerce 中基于付款方式 ID 的其他电子邮件收件人

Additional email recipient based on payment method Ids in WooCommerce

我正在尝试根据 WooCommerce“新订单”电子邮件通知中的付款方式 ID 添加其他电子邮件收件人。

这是我的代码:

function at_conditional_admin_email_recipient($recipient, $order){
 //   if( ! is_a($order, 'WC_Order') ) return $recipient;
   
    if ( get_post_meta($order->id, '_payment_method', true) == 'my_custom_gateway_id' ) {
        $recipient .= ', xx1@xx.com';
    } else {
        $recipient .= ', xx2@xx.com';
    }
    return $recipient;
    
    
};
add_filter( 'woocommerce_email_recipient_new_order', 'at_conditional_admin_email_recipient', 10, 2 );

但是钩子似乎没有触发我的功能。可能是什么原因?

您的代码自 Woocommerce 3 以来已过时,请尝试以下操作:

add_filter( 'woocommerce_email_recipient_new_order', 'payment_id_based_new_order_email_recipient', 10, 2 );
function payment_id_based_new_order_email_recipient( $recipient, $order ){
    // Avoiding backend displayed error in Woocommerce email settings (mandatory)
    if( ! is_a($order, 'WC_Order') ) 
        return $recipient;

    // Here below set in the array the desired payment Ids
    $targeted_payment_ids = array('bacs');
   
    if ( in_array( $order->get_payment_method(), $targeted_payment_ids ) ) {
        $recipient .= ', manager1@gmail.com';
    } else {
        $recipient .= ', manager2@gmail.com';
    }
    return $recipient;
}

代码进入您的活动子主题(或活动主题)的 functions.php 文件。它应该有效。

Also sometimes the problem can be related to a wrong payment method Id string in your code (so try first for example with WooCommerce "cod" or "bacs" payment methods ids, to see if the code works).