将收到的订单 woocommerce 发送给经销商发送电子邮件通知 [英] Dispatch received orders woocommerce to dealers sending email notifications

查看:66
本文介绍了将收到的订单 woocommerce 发送给经销商发送电子邮件通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个电子邮件列表(经销商),当我在 wp-admin 中收到订单时,我需要打开此订单并将此订单发送给经销商(商业、用户...).每个经销商都有一封电子邮件,并在他已发送给该经销商的自定义字段中标记此订单.

I have a list of emails (dealers) and I need when I receive order in wp-admin i open this order and send this order to a dealer (commercial , user...). Every dealer have an email and mark this order in custom field that he have been send to this dealer.

在我的 woocommerce 订单页面中,我需要打开一个订单并执行以下操作:

In my woocommerce orders page I need to open a order and do something like this:

  • 订单 001 --- > 发送至 Email1@exemple.com = 订单 001 - 发送至 Email1@exemple.com
  • 订单 002 ----> 发送至 Email2@exemple.com = 订单 002 - 发送至 Email2@exemple.com
  • 订单 003 --- > 发送至 Email1@exemple.com = 订单 003 - 发送至 Email1@exemple.com

我不知道从哪里开始.

有没有人有想法或一些代码来实现这样的目标?

Does anyone have an idea or some code to achieve something like this?

谢谢

推荐答案

这里有一个完整的答案,可以满足您的需求.您必须在第二个函数中设置经销商列表中的电子邮件和姓名数组.

Here is a complete answer that will feet your needs. You will have to set in the 2nd function the array of the emails and names from your dealer list.

此代码将在后端订单编辑页面中显示一个带有选择器的自定义元框,如果您将设置经销商并单击保存订单"...

This code will display in backend Order edit pages a custom metabox with a selector, were you will set a dealer and you will click on "Save Order"…

新订单通知电子邮件只会发送到该经销商电子邮件地址一次.

A New Order notification email will be sent just once to that dealer email address.

代码如下:

//Adding Meta container admin shop_order pages
add_action( 'add_meta_boxes', 'my_custom_order_meta_box' );
if ( ! function_exists( 'my_custom_order_meta_box' ) )
{
    function my_custom_order_meta_box()
    {
        global $woocommerce, $order, $post;

        add_meta_box( 'dealer_dispatch', __('Dealer Dispatch','woocommerce'), 'add_order_custom_fields_for_packaging', 'shop_order', 'side', 'core' );
    }
}


//adding Meta field in the meta container admin shop_order pages
if ( ! function_exists( 'add_order_custom_fields_for_packaging' ) )
{
    function add_order_custom_fields_for_packaging()
    {
        global $woocommerce, $order, $post;

        // Define HERE your array of values  <==  <==  <==  <==  <==  <==  <==  <==
        $option_values = array(
            'default'               => __('no selection', 'woocommerce'),
            'dealer1@email.com'     => 'Dealer 1 Name',
            'dealer2@email.com'     => 'Dealer 2 Name',
            'dealer3@email.com'     => 'Dealer 3 Name',
        );

        // Get the values from the custom-fields (if they exist)
        $meta_field_data = get_post_meta( $post->ID, '_dealer_dispatch', true );
        $dealer_email_sent = get_post_meta( $post->ID, '_dealer_email_sent', true );

        echo '<input type="hidden" name="my-custom-order_meta-box-nonce" value="'. wp_create_nonce() .'">

            <label for="dealer_dispatch">'.__('Select a dealer', 'woocommerce').'</label><br>
            <select name="dealer_dispatch">';

                    foreach( $option_values as $option_key => $option_value ){

                        if ( $meta_field_data == $option_key || 'default' == $option_key )
                            $selected = ' selected';
                        else
                            $selected = '';

                        echo '<option value="'.$option_key.'"'.$selected.'>'. $option_value.'</option>';
                    }

            echo '</select><br>';

        // if an email has been sent to the dealer we display a message
        if( ! empty($dealer_email_sent) )
            echo '<p style="color:green; font-weight:bold;">'.__('Email sent to: ', 'woocommerce').$dealer_email_sent.'</p>';
    }
}


//Save the data of the Meta field
add_action( 'save_post', 'add_my_custom_field_for_order_meta_box', 20, 1 );
if ( ! function_exists( 'add_my_custom_field_for_order_meta_box' ) )
{

    function add_my_custom_field_for_order_meta_box( $post_id ) {

        ## Verify and securing data. ##

        // Check if our nonce is set.
        if ( ! isset( $_POST[ 'my-custom-order_meta-box-nonce' ] ) ) {
            return $post_id;
        }
        $nonce = $_REQUEST[ 'my-custom-order_meta-box-nonce' ];

        //Verify that the nonce is valid.
        if ( ! wp_verify_nonce( $nonce ) ) {
            return $post_id;
        }

        // Continuing only if form is submited.
        if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
            return $post_id;
        }

        // Check and set the user's permissions.
        if ( 'page' == $_POST[ 'post_type' ] ) {

            if ( ! current_user_can( 'edit_page', $post_id ) ) {
                return $post_id;
            }
        } else {

            if ( ! current_user_can( 'edit_post', $post_id ) ) {
                return $post_id;
            }
        }

        //  --  --  IT IS SECURED NOW  --  --

        // Sanitize input and update order meta data custom field.
        $dealer_dispatch = $_POST[ 'dealer_dispatch' ];

        // Saving the selected value
        if( 'default' != $dealer_dispatch )
            update_post_meta( $post_id, '_dealer_dispatch', sanitize_text_field( $dealer_dispatch ) );


        # SEND CUSTOM EMAIL ONLY ONCE #

        $dealer_dispatch_val = get_post_meta( $post_id, '_dealer_dispatch', true );
        $dealer_email_sent = get_post_meta( $post_id, '_dealer_email_sent', true );

        if( empty($dealer_email_sent) && !empty($dealer_dispatch_val) ){

            $email_notifications = WC()->mailer()->get_emails();
            $email_notifications['WC_Email_New_Order']->recipient = $dealer_dispatch;
            $email_notifications['WC_Email_New_Order']->trigger( $post_id );

            // Creating a custom meta data for this order to avoid sending this email 2 times
            update_post_meta( $post_id, '_dealer_email_sent', $dealer_dispatch_val );
        }
    }
}

代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.

此代码已经过测试且有效.

This code is tested and works.

这篇关于将收到的订单 woocommerce 发送给经销商发送电子邮件通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆