[英]Conditional custom New Order Woocommerce email notification on different recippients
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).
有2个超级管理员(ID:admin),5个管理员(ID:admin2),15个银行客户(ID:银行)和2个房地产客户(ID:房地产)。 I have already setup the custom users with the ID's.
我已经用ID设置了自定义用户。 No products have pricing.
没有产品有定价。 Everything is invoiced in an email with the order_info.
一切都以带有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. 我不是最擅长php的,以前的工作很草率。
Any help? 有什么帮助吗?
You can use wc hook as follows in functions.php filr 您可以在functions.php filr中如下使用wc挂钩
add_action( 'woocommerce_new_order', 'send_email_to_admin', 1, 1 ); add_action('woocommerce_new_order','send_email_to_admin',1,1); function send_email_to_admin($order_id) { // use order id to find user id and send email }
函数send_email_to_admin($ order_id){//使用订单ID查找用户ID并发送电子邮件}
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. 使用挂在
woocommerce_thankyou
操作钩中的自定义函数,将使您能够基于用户角色和自定义多个收件人来进行条件电子邮件通知。
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'
您还需要检查3个if语句中的用户角色是否匹配……房地产ID通常不能有空格,应改为:
'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. 一旦触发此自定义电子邮件,将为订单设置一个自定义字段
'_custom_emails_sent'
。
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(
'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' );
}
}
Code goes in function.php file of your active child theme (or active theme). 代码进入您的活动子主题(或活动主题)的function.php文件中。
Tested and works 经过测试和工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.