lorro

WooCommerce - Add order notes column to orders list

Apr 30th, 2020
1,767
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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
RAW Paste Data