Okay I have had a question come from a client that doesn't seem impossible, but correctly setting up the conditions is a problem for me. Here is what is happening and what I need. i would really this to be a function of possible.
The store has a user hierarchy. There are 2x Super Admins (ID: admin), 5x Admins (ID: admin2), 15x Bank Customers (ID: banks), and 2x Real Estate customers (ID: real estate). I have already setup the custom users with the ID's. No products have pricing. Everything is invoiced in an email with the order_info. Everything is paid at a later date in house. So the site really relies on emails.
I am not the greatest with php and what I had previous was very sloppy.
Any help?
Using a custom function hooked in woocommerce_thankyou
action hook, will allow you to make that conditional email notifications based on user roles and on custom multiple recipients.
You will need to replace admins and super admins email in this function.
You will need also to check that the users roles are matching in the 3 if statements… The ID for Real estate can't have a space normally and should be instead: 'real_estate'
This will send email notifications for orders that have a status like 'on-hold', 'pending', 'processing' or 'completed'...
Once this custom email will be triggered a custom field '_custom_emails_sent'
will be set for the order.
Here is the code:
add_action( 'woocommerce_thankyou', 'custom_new_order_email_notifications', 10, 1 );
function custom_new_order_email_notifications( $order_id ){
// If Custom Emails already sent we exit
if( get_post_meta( $order_id, '_custom_emails_sent', true ) ) return;
$targeted_statuses = array( 'wc-on-hold', 'wc-pending', 'wc-processing', 'wc-completed' );
$order_status = get_post_status( $order_id );
// Only for the correct order statuses; If not we exit
if( ! in_array( $order_status, $targeted_statuses ) ) return;
// HERE (below) replace super admins and admins REAL emails
$super_admin_emails = array(
'[email protected]', '[email protected]' );
$admin_emails = array(
'[email protected]', '[email protected]',
'[email protected]', '[email protected]', '[email protected]' );
// Get the user of the order
$user_id = get_post_meta( $order_id, '_customer_user', true );
$user = get_userdata( $user_id );
$recipient = '';
// 1. Bank Customers user role
if( in_array('banks', $user->roles) ){
$recipients = implode(',', $admin_emails);
}
// 2. Real estate Customers user role
if( in_array('real_estate', $user->roles) ){
$recipients = implode(',', $admin_emails);
}
// 3. Admins Customers user role
if( in_array('admin2', $user->roles) ){
$recipients = implode(',', $admin_emails);
}
// Sending new order email notification to the targeted recipients
if( '' != $recipients ){
$mailer = WC()->mailer()->get_emails();
$mailer['WC_Email_New_Order']->recipient = $recipients;
$mailer['WC_Email_New_Order']->trigger( $order_id ); // sending
// We set a custom field that will avoid repetitive sends
update_post_meta( $order_id, '_custom_emails_sent', '1' );
}
}
Code goes in function.php file of your active child theme (or active theme).
Tested and works