lorro

WooCommerce - Add order notes column to orders list #2

Nov 16th, 2020
488
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.   // 'Actions' to remain as last column
  4.   // show only the last private note
  5.   // Code goes in functions.php for your child theme
  6.   // not tested in a PHP snippet plugin
  7.  
  8.   // Add column "Order Notes" on the orders page
  9.   add_filter( 'manage_edit-shop_order_columns', 'add_order_notes_column' );
  10.   function add_order_notes_column( $columns ) {
  11.     $new_columns = ( is_array( $columns ) ) ? $columns : array();
  12.     unset( $new_columns['wc_actions'] );
  13.     $new_columns['order_notes'] = 'Order Notes';
  14.     $new_columns['wc_actions'] = 'Actions';
  15.     return $new_columns;
  16.   }
  17.  
  18.   add_action( 'admin_print_styles', 'add_order_notes_column_style' );
  19.   function add_order_notes_column_style() {
  20.     $css = '.post-type-shop_order table.widefat.fixed { table-layout: auto; width: 100%; }';
  21.     $css .= 'table.wp-list-table .column-order_notes { min-width: 280px; text-align: left; }';
  22.     $css .= '.column-order_notes ul { margin: 0 0 0 18px; list-style-type: disc; }';
  23.     $css .= '.order_customer_note { color: #ee0000; }'; // red
  24.     $css .= '.order_private_note { color: #0000ee; }'; // blue
  25.     wp_add_inline_style( 'woocommerce_admin_styles', $css );
  26.   }
  27.  
  28.   // Add order notes to the "Order Notes" column
  29.   add_action( 'manage_shop_order_posts_custom_column', 'add_order_notes_content' );
  30.   function add_order_notes_content( $column ) {
  31.     if( $column != 'order_notes' ) return;      
  32.     global $post, $the_order;
  33.     if( empty( $the_order ) || $the_order->get_id() != $post->ID ) {
  34.       $the_order = wc_get_order( $post->ID );
  35.     }    
  36.     $args = array();
  37.     $args['order_id'] = $the_order->get_id();
  38.     $args['order_by'] = 'date_created';
  39.     $args['order'] = 'DESC';
  40.     $notes = wc_get_order_notes( $args );
  41.     if( $notes ) {
  42.       print '<ul>';
  43.       foreach( $notes as $note ) {
  44.         if( ! $note->customer_note ) {
  45.           print '<li class="order_private_note">';
  46.           $date = date( 'd/m/y H:i', strtotime( $note->date_created ) );
  47.           print $date.' by '.$note->added_by.'<br>'.$note->content.'</li>';
  48.           break;
  49.         }
  50.       }
  51.       print '</ul>';
  52.     }
  53.   } // end function
RAW Paste Data