简体   繁体   English

在不同的收件人上有条件的自定义新订单Woocommerce电子邮件通知

[英]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. 因此,该网站确实依赖电子邮件。

  1. If the 15x Bank Customers place a new_order, these new_order(s) need to send the email invoice to the 5x Admins. 如果15x银行客户下了新订单,则这些new_order需要将电子邮件发票发送给5x管理员。
  2. If the 2x Real Estate customers place a new_order, these new_order(s) need to send the email invoice to the 5x Admins. 如果2x房地产客户下了new_order,则这些new_order需要将电子邮件发票发送给5x管理员。
  3. If the 5x Admins place a new_order, these new_order(s) need to send the email invoice to the 2x Super Admins. 如果5x管理员下了一个new_order,则这些new_order需要将电子邮件发票发送给2x超级管理员。

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.

相关问题 WooCommerce 中新订单电子邮件通知的自定义主题 - Custom subject for New Order Email notification in WooCommerce 在新订单Woocommerce电子邮件通知中添加自定义链接 - Add a custom link in new order Woocommerce email notification Woocommerce将新订单通知发送到不同的电子邮件(基于运送国家) - Woocommerce new order notification to different email based on shipping country 将产品自定义字段中的收件人添加到Woocommerce新订单电子邮件通知中 - Add recipients from product custom fields to Woocommerce new order email notification 将订单总重量(吨)转换为 WooCommerce 新订单 email 通知 - Convert the order total weight in tons to WooCommerce new order email notification 将订单总重量添加到WooCommerce新订单电子邮件通知中 - Add the order total weight to WooCommerce new order email notification 自定义标题和主题 email 通知 WooCommerce 中的自定义订单状态 4 - Custom heading and subject email notification for custom order status in WooCommerce 4 将电子邮件收件人添加到 Woocommerce 中本地取货的新订单通知 - Add email recipients to New order notification for Local pickup in Woocommerce 在Woocommerce中将订单交易ID添加到管理员新订单电子邮件通知中 - Add orders transaction id to admin new order email notification in Woocommerce 基于 Woocommerce 新订单通知的配送方式 ID 的电子邮件收件人 - Email recipients based on Shipping method Id for Woocommerce new order notification
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM