-2

Hi is it possible to make an extra field in backend, when orders are selected and marked as shipped then an extra field come and where i can select drivers who are responsible for this shipped order .Is that possible? is there any hook available ?

https://wordpress.org/plugins/woocommerce/

Shipped is a custom order status we created.

my need is when a order marked as shipped then need to appear extra input text or select box where i can select driver name**(user with role is driver )** and save it .

Can i do it using wc_order_statuses filter ? please help to do this .

i know about custom post meta and it's working .But i want to update the value only when order status changed to shipped . Is there any hook available for this .When my staff marked 5,6 order marked as shipped in bulk edit section then a popup need to appear and it say select driver,. How to do this

closed as off-topic by cybmeta, Mayeenul Islam, Pieter Goosen, TheDeadMedic, Howdy_McGee Jan 5 '16 at 15:00

This question appears to be off-topic. The users who voted to close gave this specific reason:

If this question can be reworded to fit the rules in the help center, please edit the question.

  • When you say marked as shipped did you mean changing order status to Completed ? Because there is no status called Shipped. Please clarify. – Prasad Nevase Jan 3 '16 at 6:47
  • Yes, you can have custom post meta for every order. In the custom post meta prepopulate with drivers and then select one. – WPTC-Troop Jan 3 '16 at 8:10
  • @ItsMePN Shipped is a custom order status we created. my need is when a order marked as shipped then need to appear extra input text or select box where i can select driver name**(user with role is driver )** and save it . Can i do it using wc_order_statuses filter ? please help to do this – tom Jan 3 '16 at 8:22
  • @WPTC-Troop hi, i know about custom post meta and it's working .But i want to update the value only when order status changed to shipped . Is there any hook available for this .When my staff marked 5,6 order marked as shipped in bulk edit section then a popup need to appear and it say select driver,. How to do this – tom Jan 3 '16 at 8:24
  • In that case hook a function on order status change. Whenever an order status changed to shipped then let the Driver section available to select driver. The same can be done for bulk edit too. – WPTC-Troop Jan 3 '16 at 11:40
1

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' );
  • Thank you very much for this answer .I have a doubt where you write the condition for to check the updated order status is shipped. – tom Jan 3 '16 at 13:08
  • Hi i think the code you are given is perfect . But i didn't see driver meta box in the current screen both in bulk action or single order detail page when update the status to shipped. I change the code "shipped" == get_post_status(get_query_var('post')) and shipped is my order status .Please help – tom Jan 3 '16 at 13:22
  • The meta box shall appear in side panel under Order Actions if the order status is shipped and if you have correctly updated it in the code. Besides that you also need to change $drivers = get_users( 'role=subscriber' ); to $drivers = get_users( 'role=driver' ); Make sure the role string which you pass is exactly what you have used while registering driver user role. – Prasad Nevase Jan 3 '16 at 14:51
  • Could you please tell me if there any way to do this when the staff is using bulk edit. ie when they select 5, 6 orders together and marked it as shipped then a popup appear and it say select a driver . If that possible please reply. Currently for selecting drivers we need to enter each order individually . – tom Jan 3 '16 at 15:12
  • Creating a popup will be complex though not impossible. I suggest adding Driver drop-down field to quick edit section. That will allow you to update the driver for multiple orders at a time. However first you will need to update the order status and then you can update the driver for multiple orders. Please see this link https://codex.wordpress.org/Plugin_API/Action_Reference/quick_edit_custom_box it provides valuable information on how to add a custom field to quick edit section. – Prasad Nevase Jan 3 '16 at 15:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.