Sending received woocommerce orders to dealers sending email notifications

I have a list of emails (dealers) and I need, when I receive an order in wp-admin, I open this order and send this order to the dealer (commercial user ...). Each dealer has an email and mark this order in the custom box he sent to that dealer.

On my woocommerce orders page, I need to open an order and do something like this:

  • Order 001 ---> Email 1@exemple.com = Order 001 - Email 1@exemple.com
  • Order 002 ----> Email 2@exemple.com = Order 002 - Email 2@exemple.com
  • Order 003 ---> Email 1@exemple.com = Order 003 - Email 1@exemple.com

I don't know where to start.

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

thank

+3


source to share


1 answer


Here is a complete answer that will suit your needs. You will need to set in the 2nd function an array of letters and names from your dealer list.

This code will display a special metaxim with a selector on the order edit pages for the order if you installed a dealer and you clicked "Save Order" ...

Custom Edit Order metabolox

A new order notification email will only be sent once to the dealer's email address.



Here is the code:

//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 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 );
        }
    }
}

      

The code goes in the function.php file of your active child theme (or theme), as well as any plugin file.

This code has been tested and works.

+5


source







All Articles