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.
This plugin allows you to enter an Admin note on the order and then displays this note on order report screen.
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:
- display the important Order Note input field on Woocommerce edit order screen
- save it when needed (i.e. when you save the order)
- 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | 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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | 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.
Disclaimer: All content on this site, is use at your own risk (Always backup before changing anything in your software/database/servers etc). Techs change, go out of date etc...I/we accept no liability if anything you use on this site adversely affects you.
Recent Comments