使用在woocommerce_thankyou 操作挂钩中挂钩的自定义函数,将允许您根据用户角色和自定义多个收件人发出有条件的电子邮件通知。
您需要在此功能中替换管理员和超级管理员的电子邮件。
您还需要检查 3 个 if 语句中的用户角色是否匹配……房地产的 ID 通常不能有空格,应该改为:'real_estate'
这将为状态为“暂停”、“待处理”、“处理中”或“已完成”的订单发送电子邮件通知...
一旦触发此自定义电子邮件,将为订单设置自定义字段'_custom_emails_sent'。
代码如下:
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(
'supadmin1@example.com', 'supadmin2@example.com' );
$admin_emails = array(
'admin1@example.com', 'admin2@example.com',
'admin3@example.com', 'admin4@example.com', 'admin5@example.com' );
// 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' );
}
}
代码进入您的活动子主题(或活动主题)的 function.php 文件中。
经过测试并且可以工作