0

I want to stop send Woocommerce email for specific user/email. this is my example code to stop send email when order is completed.

<?php
add_filter( 'woocommerce_email_headers', 'ieo_ignore_function', 10, 2);

function ieo_ignore_function($headers, $email_id, $order) {
    $list = 'admin@example.com,cs@example.com';
    $user_email = (method_exists( $order, 'get_billing_email' ))? $order->get_billing_email(): $order->billing_email;
    $email_class = wc()->mailer();
    if($email_id == 'customer_completed_order'){
        if(stripos($list, $user_email)!==false){
            remove_action( 'woocommerce_order_status_completed_notification', array( $email_class->emails['WC_Email_Customer_Completed_Order'], 'trigger' ) );
        }
    }
}

But WP keep send the email. I try to search in Woocommerce docs and source (github) and Stackoverflow also but still cant solve this.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
plonknimbuzz
  • 2,594
  • 2
  • 19
  • 31

2 Answers2

3

In this example "Customer completed order" notification is disable for specific customer email addresses:

// Disable "Customer completed order" for specifics emails
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'completed_email_recipient_customization', 10, 2 );
function completed_email_recipient_customization( $recipient, $order ) {
    // Disable "Customer completed order
    if( is_a('WC_Order', $order) && in_array($order->get_billing_email(), array('jack@mail.com','emma@mail.com') ) ){
        $recipient = '';
    }
    return $recipient;
}

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

Note: A filter hook needs always to return the filtered main function argument


It can also be done from User IDs like:

// Disable "Customer completed order" for specifics User IDs
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'completed_email_recipient_customization', 10, 2 );
function completed_email_recipient_customization( $recipient, $order ) {
    // Disable "Customer completed order
    if( is_a('WC_Order', $order) && in_array($order->get_customer_id(), array(25,87) ) ){
        $recipient = '';
    }
    return $recipient;
}

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


Similar: Stop specific customer email notification based on payment methods in Woocommerce

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
0

While answer above is working, this seems to be more straighforward solution:

add_filter( 'woocommerce_email_recipient_customer_completed_order', 'completed_email_recipient_customization', 10, 2 );
function completed_email_recipient_customization( $recipient, $order ) {
    if( in_array($recipient, ['some@email2block.com', 'another@email2block.com'] ) ) {
        $recipient = '';
    }
    return $recipient;
}
OnGe
  • 1