Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

php - Stop specific customer email notification based on payment methods in Woocommerce

In Woocommerce, I need to stop email notifications sent to the customer when order place except when payment_method is BACS (Direct bank transfer).

I tried following in my active theme's function.php file:

add_filter( 'woocommerce_email_recipient_customer_on_hold_order_order', 'customer_order_email_if_bacs', 10, 2 );

function customer_order_email_if_bacs( $recipient, $order ) {

    if( $order->payment_method() !== 'bacs' ) $recipient = '';

    return $recipient;
}

But it don't work. Any help is appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Update 2

The woocommerce_email_recipient_{$email_id} filter is a composite hook and the right email ID to set in it is customer_on_hold_order and not customer_on_hold_order_order which will not work…

With the WC_Order object, since Woocommerce 3, you need to use get_payment_method() method.

To avoid Customer ON Hold email notification except for "Bacs" payment method use:

add_filter( 'woocommerce_email_recipient_customer_on_hold_order', 'customer_on_hold_order_for_bacs', 10, 2 );
function customer_on_hold_order_for_bacs( $recipient, $order ) {

    if( is_a('WC_Order', $order) && $order->get_payment_method() !== 'bacs' ){
        $recipient = '';
    }
    return $recipient;
}

Code goes in function.php file of your active child theme (active theme). Tested and works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...