Advertisement
majeedraza

WooCommerce - Add order notes column to orders list

Apr 30th, 2020
4,308
0
Never
9
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.06 KB | None | 0 0
  1. <?php
  2.   // WooCommerce - Add order notes column to orders list
  3.   // Code goes in functions.php for your child theme
  4.   // not tested in a PHP snippet plugin
  5.  
  6.   // Add column "Order Notes" on the orders page
  7.   add_filter( 'manage_edit-shop_order_columns', 'add_order_notes_column' );
  8.   function add_order_notes_column( $columns ) {
  9.     $new_columns = ( is_array( $columns ) ) ? $columns : array();
  10.     $new_columns['order_notes'] = 'Order Notes';
  11.     return $new_columns;
  12.   }
  13.  
  14.   add_action( 'admin_print_styles', 'add_order_notes_column_style' );
  15.   function add_order_notes_column_style() {
  16.     $css = '.post-type-shop_order table.widefat.fixed { table-layout: auto; width: 100%; }';
  17.     $css .= 'table.wp-list-table .column-order_notes { min-width: 280px; text-align: left; }';
  18.     $css .= '.column-order_notes ul { margin: 0 0 0 18px; list-style-type: disc; }';
  19.     $css .= '.order_customer_note { color: #ee0000; }'; // red
  20.     $css .= '.order_private_note { color: #0000ee; }'; // blue
  21.     wp_add_inline_style( 'woocommerce_admin_styles', $css );
  22.   }
  23.  
  24.   // Add order notes to the "Order Notes" column
  25.   add_action( 'manage_shop_order_posts_custom_column', 'add_order_notes_content' );
  26.   function add_order_notes_content( $column ) {
  27.     if( $column != 'order_notes' ) return;      
  28.     global $post, $the_order;
  29.     if( empty( $the_order ) || $the_order->get_id() != $post->ID ) {
  30.       $the_order = wc_get_order( $post->ID );
  31.     }    
  32.     $args = array();
  33.     $args['order_id'] = $the_order->get_id();
  34.     $args['order_by'] = 'date_created';
  35.     $args['order'] = 'ASC';
  36.     $notes = wc_get_order_notes( $args );
  37.     if( $notes ) {
  38.       print '<ul>';
  39.       foreach( $notes as $note ) {
  40.         if( $note->customer_note ) {
  41.           print '<li class="order_customer_note">';
  42.         } else {
  43.           print '<li class="order_private_note">';
  44.         }
  45.         $date = date( 'd/m/y H:i', strtotime( $note->date_created ) );
  46.         print $date.' by '.$note->added_by.'<br>'.$note->content.'</li>';
  47.       }
  48.       print '</ul>';
  49.     }
  50.   } // end function
Advertisement
Comments
  • dashastone
    1 year
    # text 0.16 KB | 0 0
    1. Hi - how would this code look if I only want to display author private comments and not the ones the system includes (i.e. "changed status to....")? Great code!
  • majeedraza
    1 year
    # text 0.33 KB | 0 0
    1. lines 39 - 46 become:
    2. foreach( $notes as $note ) {
    3. if( $note->customer_note ) {
    4. } else {
    5. print '<li class="order_private_note">';
    6. $date = date( 'd/m/y H:i', strtotime( $note->date_created ) );
    7. print $date.' by '.$note->added_by.'<br>'.$note->content.'</li>';
    8. }
    9. }
    10.  
    • agentollie66
      1 year
      # text 0.16 KB | 0 0
      1. Hi Lorro I tried this change (39-46) and it didn't alter anything?
      2. If possible would only like to see customer or staff manual notes (not system notes)
      3. Thanks!
  • majeedraza
    1 year
    # text 0.53 KB | 0 0
    1. Works for me, sorry I can't debug it on another site via comment/post communication.
    2.  
    3. To exclude customer notes and private notes generated by 'system', lines 39 - 46 become:
    4. foreach( $notes as $note ) {
    5. if( ! $note->customer_note ) {
    6. if( $note->added_by != 'system' ) {
    7. print '<li class="order_private_note">';
    8. $date = date( 'd/m/y H:i', strtotime( $note->date_created ) );
    9. print $date.' by '.$note->added_by.'<br>'.$note->content.'</li>';
    10. }
    11. }
    12. }
  • dishdoctor
    1 year
    # text 0.18 KB | 0 0
    1. Is there a way to only show one system generated note that has been filtered by keyword or phrase.
    2. Example only show:
    3. 13/12/20 15:41 by system
    4. "Answer to the Secret Question": qkgjfkq2i4
  • majeedraza
    1 year (edited)
    # text 0.20 KB | 0 0
    1. The PHP function strpos() can do that.
    2.  
    3. if( strpos( $note->content, 'Answer to the Secret Question' ) === 0 ) {
    4.  
    5. The === means the test string must be at the beginning of the content to give TRUE.
  • dishdoctor
    1 year
    Comment was deleted
  • dishdoctor
    1 year
    # text 0.04 KB | 0 0
    1. Thanks! Can you show me add this to the code?
  • majeedraza
    1 year
    # text 0.04 KB | 0 0
    1. Sorry, I have other commitments at the moment.
Add Comment
Please, Sign In to add comment
Advertisement