Woocommerce – Add an Important Admin Note to the Order Report for Order pickers to view

In this article I’ll show you how to add an Important Admin Note to the Order Report, very useful if there’s some information you want to add for Order pickers to view. For instance maybe the item is fragile you can highlight this to the order picker.

 

Screen Shot 2016-01-14 at 15.21.07

This plugin allows you to enter an Admin note on the order and then displays this note on order report screen.

Screen Shot 2016-01-14 at 15.23.27

If you just want to get the plugin please click here to download the plugin. The rest of this article will run over how to create a plugin like this, so if you have an interest int he programming side of wordpress, this is for you.

Essentially to write this plugin we need todo 3 things:

  1. display the important Order Note input field on Woocommerce edit order screen
  2. save it when needed (i.e. when you save the order)
  3. display the note (in red in the graphic above) on the Woocommerce Order Report

So lets dive in:

Display the important Order Note input field on Woocommerce edit order screen

To display the order note field we need to do something like this (below). Print out the html for the text input field and add some explanatory note for the admin user.

add_action('add_meta_boxes',function(){
    add_meta_box('greenbox_important_order_notes', 'Important Order Note (displayed only to admin)', 'greenbox_important_order_note_callback','shop_order','normal','high');
});

function greenbox_important_order_note_callback($post){
    	// Add a nonce field so we can check for it later.
	wp_nonce_field( 'greenbox_important_order_note_meta_box', 'greenbox_important_order_note_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, '_greenbox_important_note', true );

	echo '<label for="greenbox_important_order_notes">';
	echo 'Note:';
	echo '</label> ';
	echo '<input type="text" id="greenbox_important_order_notes" name="greenbox_important_order_note" value="' . esc_attr( $value ) . '" style="width:80%; border: 1px solid green; " />';
}

 

Save it when needed (i.e. when you save the order)

In the code below I’m saving the Import Order Note, and before doing so checking that its valid.

function greenbox_save_meta_box_data_important_note( $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['greenbox_important_order_note_meta_box_nonce'] ) ) {
		return;
	}

	// Verify that the nonce is valid.
	if ( ! wp_verify_nonce( $_POST['greenbox_important_order_note_meta_box_nonce'], 'greenbox_important_order_note_meta_box' ) ) {
		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'] ) {
            // its ok
        }
        else{
            return; // its not ok
        }



	/* OK, it's safe for us to save the data now. */
	
	// Make sure that it is set.
	if ( ! isset( $_POST['greenbox_important_order_note'] ) ) {
		return;
	}

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

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

 

finally

Display the note (in red in the graphic above) on the Woocommerce Order Report

In this function I use the relevant hook to drop my Import Order Note into the Order Report in the correct place.

function greenbox_order_extra_columns_content($column) {
    global $post;

    $order_id = $post->ID;
    
    
    
    //get_post_meta( $order_id, $key);

    /* show all meta 
    $meta = get_post_meta($order_id);
    $greenbox_comment_for_order="";
    foreach ($meta as $k=>$v){
     $greenbox_comment_for_order .= "<br>k = $k , v = $v ";   
    }*/
    $greenbox_note_for_order = trim(get_post_meta( $post->ID, '_greenbox_important_note', true ));
    
    switch ($column) {
        //case "order_notes":
        case "shipping_address":
            
            if(isset($greenbox_note_for_order) && !empty($greenbox_note_for_order))
                echo "<div style='background-color: red; color: white; padding: 2px;'> $greenbox_note_for_order </div>";  //$list_of_order_notes; 
            
            break;
    }
}

add_action("manage_shop_order_posts_custom_column", "greenbox_order_extra_columns_content");

 

I hope you found that interesting, if you need any help with Programming WordPress or Woocommerce please get in touch.

19 thoughts on “Woocommerce – Add an Important Admin Note to the Order Report for Order pickers to view”

  1. You save my life man! I have search 2 days on google and finally I found your article! I wonder why you don’t upload this plugin to wordpress page so everyone can find it easily!

    BTW how can I add more custom field ?

    Reply
    • hi David,

      glad you found it useful, yeah maybe I’ll put it on WordPress as a plugin one of the days ( never enough time for these things…)

      if you have some alittle programming knowledge you can setup another custom field in a similar way ( google wordpress how to add a custom field tutorial ). if not maybe try something like codeable to find an expert to do.

      Reply
  2. Hi,
    I’m testing this out on a client site running WordPress 5.2 and WooCommerce 3.6.2.
    The plugin activates and each order has a field for the Important Order Note. That part works well. But on the Order overview list, any new Important Order Note is not added to the dom.
    Any thoughts on what might be going on?
    Thanks!

    Reply
  3. hi Dave,

    At a guess I would say that maybe Woocommerce doesn’t call the relevant call back ( do_action) any more. I know there have been changes to the order report lately by Woocommerce.

    So this probably needs to be called somewhere else I guess: add_action(“manage_shop_order_posts_custom_column”, “greenbox_order_extra_columns_content”);

    I’ll try and come back and update the code at some point when I get some time ….

    Reply
    • Sorry to hear that Mark. Have you tried this on a vanilla site to ensure something else is not interfering with it ( ie use 2020 theme and switch off all plugins except Woocommerce ).

      I haven’t looked at this plugin in some years, but I may well do an updated version one of the days (time is the great enemy, very busy with client work at the moment ).

      Reply
  4. thanks for the reply, looking at this article, it has been a few years already , 4 years to be exact and looking around for a plugin to address our issue and this plugin should be perfect if it worked displaying admin notes on the order report screen.

    the text field do appear on the order but it is not displayed in the order page.

    Reply
  5. Great little plugin.

    Seems to trigger this notice though, is there a way to resolve this, please?

    PHP Notice: Trying to get property ‘post_type’ of non-object in /home/public_html/wp-includes/class-wp-query.php on line 4179

    Reply
  6. Would be great to see the ability to enter in notes on order and then select background color so when looking at order list we can easily distinguish some orders from other orders.

    Reply
    • thats a great idea Matt, and maybe have a key to look up the colour.

      some I don’t have time for at mo, but a great idea none the less.

      Reply
  7. this plugin seems to not be working anymore on our site. all notes are gone. the field is still there in the order screen, but it doesnt show in the order list view anymore 🙁 all of our data is probably not gone, but we cant see it 🙁 we relied on this quite a bit to track customers on how many orders they have made.

    Reply

Leave a Comment