Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...