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

问题描述

我正在尝试根据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文件中。应该可以。

有时,问题也可能与代码中的付款方式ID字符串错误有关(因此,请首先尝试使用WooCommerce的“ cod”或“ bacs”付款方式ID,以查看代码是否有效)。