Advertisement
Guest User

WooCommerce - Add order notes column to orders list (rev)

a guest
Nov 15th, 2020
336
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.21 KB | None | 0 0
  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. write_log( $columns );
  12. $new_columns = ( is_array( $columns ) ) ? $columns : array();
  13. unset( $new_columns['wc_actions'] );
  14. $new_columns['order_notes'] = 'Order Notes';
  15. $new_columns['wc_actions'] = 'Actions';
  16. return $new_columns;
  17. }
  18.  
  19. add_action( 'admin_print_styles', 'add_order_notes_column_style' );
  20. function add_order_notes_column_style() {
  21. $css = '.post-type-shop_order table.widefat.fixed { table-layout: auto; width: 100%; }';
  22. $css .= 'table.wp-list-table .column-order_notes { min-width: 280px; text-align: left; }';
  23. $css .= '.column-order_notes ul { margin: 0 0 0 18px; list-style-type: disc; }';
  24. $css .= '.order_customer_note { color: #ee0000; }'; // red
  25. $css .= '.order_private_note { color: #0000ee; }'; // blue
  26. wp_add_inline_style( 'woocommerce_admin_styles', $css );
  27. }
  28.  
  29. // Add order notes to the "Order Notes" column
  30. add_action( 'manage_shop_order_posts_custom_column', 'add_order_notes_content' );
  31. function add_order_notes_content( $column ) {
  32. if( $column != 'order_notes' ) return;
  33. global $post, $the_order;
  34. if( empty( $the_order ) || $the_order->get_id() != $post->ID ) {
  35. $the_order = wc_get_order( $post->ID );
  36. }
  37. $args = array();
  38. $args['order_id'] = $the_order->get_id();
  39. $args['order_by'] = 'date_created';
  40. $args['order'] = 'DESC';
  41. $notes = wc_get_order_notes( $args );
  42. if( $notes ) {
  43. print '<ul>';
  44. foreach( $notes as $note ) {
  45. if( ! $note->customer_note ) {
  46. print '<li class="order_private_note">';
  47. $date = date( 'd/m/y H:i', strtotime( $note->date_created ) );
  48. print $date.' by '.$note->added_by.'<br>'.$note->content.'</li>';
  49. break;
  50. }
  51. }
  52. print '</ul>';
  53. }
  54. } // end function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement