Order shipped by which driver[hook for woocoomerce order staus changed and popup in admin panel ] [closed]

Please go through the code below. I have tried and tested 🙂

/**
 * Adds a box to the side column on the Order edit screens. 
 * It checks if the order status is completed & then adds meta box.
 */
function add_drivers_meta_box() {

    global $current_screen;

    /* In the second condistion below, please change it to correct string for 'shipped' status */
    if ( "shop_order" == $current_screen->post_type && "wc-completed" == get_post_status(get_query_var('post'))) {
        add_meta_box(
            'shipping_drivers',
            __( 'Shipped by Driver', 'myplugin_textdomain' ),
            'add_drivers_meta_box_callback',
            'shop_order',
            'side'          
        );
    }
}
add_action( 'add_meta_boxes', 'add_drivers_meta_box' );

/**
 * Prints the box content.
 * 
 * @param WP_Post $post The object for the current post/page.
 */
function add_drivers_meta_box_callback( $post ) {

    // Add a nonce field so we can check for it later.
    wp_nonce_field( 'add_drivers_save_meta_box_data', 'add_drivers_meta_box_nonce' );

    /*
     * Use get_post_meta() to retrieve an existing value
     * from the database and use the value for the form.
     */
    $value = get_post_meta( $post->ID, '_shipping_driver_name', true );

    echo '<label for="shipping_driver_list">';
    _e( 'Select Driver Name', 'myplugin_textdomain' );
    echo '</label> ';
    /* Here you need to change the role to 'driver'. I kept it subscriber because I don't have 'driver' role registered ;-) */
    $drivers = get_users( 'role=subscriber' );

    echo '<select id="shipping_driver_list" name="shipping_driver_name" />';
        foreach ( $drivers as $driver ) {
            $value == $driver->display_name ? $selected = "selected" : $selected = "" ;
            echo '<option value="'. esc_html( $driver->display_name ) .'" ' . $selected .'>' . esc_html( $driver->display_name ) . '</option>';
        }
    echo '</select>';
}

/**
 * When the Order is saved, saves the Driver name.
 *
 * @param int $post_id The ID of the post being saved.
 */
function add_drivers_save_meta_box_data( $post_id ) {

    /*
     * We need to verify this came from our screen and with proper authorization,
     * because the save_post action can be triggered at other times.
     */

    // Check if our nonce is set.
    if ( ! isset( $_POST['add_drivers_meta_box_nonce'] ) ) {
        return;
    }

    // Verify that the nonce is valid.
    if ( ! wp_verify_nonce( $_POST['add_drivers_meta_box_nonce'], 'add_drivers_save_meta_box_data' ) ) {
        return;
    }

    // If this is an autosave, our form has not been submitted, so we don't want to do anything.
    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return;
    }

    // Check the user's permissions.
    if ( isset( $_POST['post_type'] ) && 'shop_order' == $_POST['post_type'] ) {

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

    } else {

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

    /* OK, it's safe for us to save the data now. */

    // Make sure that it is set.
    if ( ! isset( $_POST['shipping_driver_name'] ) ) {
        return;
    }

    // Sanitize user input.
    $my_data = sanitize_text_field( $_POST['shipping_driver_name'] );

    // Update the meta field in the database.
    update_post_meta( $post_id, '_shipping_driver_name', $my_data );
}
add_action( 'save_post', 'add_drivers_save_meta_box_data' );