0

I'm looking for a solution where we can disable woocommerce sending email notification when an order is done by a client from a specific customer group (user role).

I found answer about a situation what prevent sending email for a specific product-ID. Disable WooCommerce email notification for specific product. Maybe this could be possible for our 'problem' too?

Kind regards, Kees

1 Answers1

1

You can use the hook for any email and inside the callback function you can check if the user has a specific role

function change_new_order_email_recipient( $recipient, $order ) {
  global $woocommerce;
  $uid = $order->get_user_id();
  $user_meta=get_userdata($uid);
  $user_roles=$user_meta->roles;
  if(in_array('customer', $user_roles)){ // Prevent email if user role is customer
    $recipient ='';
  }
  return $recipient;
}
add_filter('woocommerce_email_recipient_customer_completed_order', 'change_new_order_email_recipient', 1, 2);

I have checked the code rapidly and is working

melvin
  • 2,571
  • 1
  • 14
  • 37
  • Hi @Melvin, is it possible to do this the other way around? To only send an email to a specific role and not to any other? – Liv Aug 16 '19 at 19:02
  • Are you saying that you need the customer completed email to be send if the user has a specific user role ? If yes, it's possible. If not can you make it clear ? – melvin Aug 17 '19 at 06:43
  • I'd like to not send the first 'order received' and also not the second 'order processed' email to any role except for the role 'customer'. The reason is that I use wholesale roles and they shouldn't receive those emails. Would that be possible? – Liv Aug 17 '19 at 10:52
  • @Liv I think the same function above can be hooked to `woocommerce_email_recipient_customer_on_hold_order` or `woocommerce_email_recipient_customer_processing_order`to meet your objective. Also change the `in_array` in if condition as you need. I haven't tested this. But i hope it will work – melvin Aug 17 '19 at 16:49
  • I found another strategy to solve my issue and something that helps downstream with our accounting software. What I'd like is to only send woocommerce emails to user account email addresses and not to the billing email address. This is discussed here https://stackoverflow.com/questions/54628979/send-woocommerce-emails-to-user-email-not-to-billing-email/54631044#comment101566077_5463104. There is no response there however, would you have an idea? – Liv Aug 23 '19 at 05:14
  • Yes. You can use that code (from the link you shared ) in the filters i mentioned in the above comments or in the filters i mentioned in the answer – melvin Aug 23 '19 at 05:19
  • On just a fast look, the code seems to work. Didn't it work ? – melvin Aug 23 '19 at 05:20
  • No when it tries to run the code (when a new order is placed or I move an order to 'in progress' from back end) it will break and throw an error: Error Details ============= An error of type E_ERROR was caused in line 67 of the file [...]/functions.php. Error message: Uncaught Error: Call to a member function get_user() on null in /home/public/sites/[...]/functions.php:67 – Liv Aug 26 '19 at 13:29
  • What is your line 67 in `functions.php` ? Is it the above code ? – melvin Aug 27 '19 at 04:57