1

I am trying to create a column that shows notes to customers. I want this column in both woocommerce admin and customer order table. I found this code and it is showing all notes in admin. How do I alter it to show only public notes I wrote to the customer? Also, how do I alter it to show on customer my order page as well?

  // Code goes in functions.php for your child theme
  // not tested in a PHP snippet plugin
  
  // Add column "Order Notes" on the orders page
  add_filter( 'manage_edit-shop_order_columns', 'add_order_notes_column' );
  function add_order_notes_column( $columns ) {
    $new_columns = ( is_array( $columns ) ) ? $columns : array();
    $new_columns['order_notes'] = 'Order Notes';
    return $new_columns;
  }
  
  add_action( 'admin_print_styles', 'add_order_notes_column_style' );
  function add_order_notes_column_style() {
    $css = '.post-type-shop_order table.widefat.fixed { table-layout: auto; width: 100%; }';
    $css .= 'table.wp-list-table .column-order_notes { min-width: 280px; text-align: left; }';
    $css .= '.column-order_notes ul { margin: 0 0 0 18px; list-style-type: disc; }';
    $css .= '.order_customer_note { color: #ee0000; }'; // red
    $css .= '.order_private_note { color: #0000ee; }'; // blue
    wp_add_inline_style( 'woocommerce_admin_styles', $css );
  }
  
  // Add order notes to the "Order Notes" column
  add_action( 'manage_shop_order_posts_custom_column', 'add_order_notes_content' );
  function add_order_notes_content( $column ) {
    if( $column != 'order_notes' ) return;      
    global $post, $the_order;
    if( empty( $the_order ) || $the_order->get_id() != $post->ID ) {
      $the_order = wc_get_order( $post->ID );
    }    
    $args = array();
    $args['order_id'] = $the_order->get_id();
    $args['order_by'] = 'date_created';
    $args['order'] = 'ASC';
    $notes = wc_get_order_notes( $args );
     if( $notes ) {
      print '<ul>';
      foreach( $notes as $note ) {
        if( $note->customer_note ) {
          print '<li class="order_customer_note">';
        } else {
          print '<li class="order_private_note">';
        }
        $date = date( 'm/d/y H:i', strtotime( $note->date_created ) );
        print $date.' by '.$note->added_by.'<br>'.$note->content.'</li>';
      }
      print '</ul>';
    }
  } // end function
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
editcafe
  • 35
  • 5
  • First the rule on stackOverFlow is one question at the time. Then what does mean *"Also, how do I alter it to show on customer my order page as well"*? – LoicTheAztec Apr 22 '21 at 05:17
  • Sorry about that! It's my first post. I want to show the exact same thing on the My Orders table for my customers to be able to see. – editcafe Apr 22 '21 at 16:52
  • What is the *"My Orders table for my customers"*. I don't catch… You maybe mean *"Customer My account Orders list"*, right? – LoicTheAztec Apr 22 '21 at 16:56
  • Yes, that's right. – editcafe Apr 22 '21 at 17:37
  • @LoicTheAztec I've posted the new question here. https://stackoverflow.com/questions/67269652/add-order-notes-to-woocommerce-my-account-orders-list I'd appreciate it if you gave it a look! Thank you! – editcafe Apr 26 '21 at 15:39

1 Answers1

2

First the rule on stackOverFlow is one question at the time.

To only display public order notes send to customer in a custom column on admin orders list, use the following revisited code:

// Add custom column on admin orders list page
add_filter( 'manage_edit-shop_order_columns', 'add_order_notes_column' );
function add_order_notes_column( $columns ) {
    $columns['order_notes'] = 'Order Notes';
    return $columns;
}

// CSS styles
add_action( 'admin_print_styles', 'add_order_notes_column_style' );
function add_order_notes_column_style() {
    $css = '.post-type-shop_order table.widefat.fixed { table-layout: auto; width: 100%; }';
    $css .= 'table.wp-list-table .column-order_notes { min-width: 280px; text-align: left; }';
    $css .= '.column-order_notes ul { margin: 0 0 0 18px; list-style-type: disc; }';
    // $css .= '.order_customer_note { color: #ee0000; }'; // red
    // $css .= '.order_private_note { color: #0000ee; }'; // blue
    wp_add_inline_style( 'woocommerce_admin_styles', $css );
}

// Admin orders list custom column displayed content
add_action( 'manage_shop_order_posts_custom_column', 'add_order_notes_content' );
function add_order_notes_content( $column ) {
    global $post, $the_order;

    if( 'order_notes' !== $column )
        return;

    $order = is_a($the_order, 'WC_Order') ? $the_order : wc_get_order( $post->ID );

    $notes = wc_get_order_notes( array(
        'order_id' => $order->get_id(),
        'order_by' => 'date_created',
        'order' => 'ASC',
    ) );

    if( ! empty($notes) ) {
        echo '<ul>';

        foreach( $notes as $note ) {
            if( $note->customer_note && 'system' !== $note->added_by ) {
                echo '<li class="order_customer_note">' . sprintf( __('%s by %s <br> %s:'),
                    date_i18n( 'm/d/y H:i', strtotime( $note->date_created ) ),
                    $note->added_by,
                    $note->content
                ) . '</li>';
            }
        }
        echo '</ul>';
    }
}

Code goes in functions.php file of the active child theme (or active theme). Tested and works.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • I tried to add the private note as well to the code, but it gives an error saying "Notice: Undefined property: stdClass::$private_note". `if ($note->private_note){ echo '
  • ' . sprintf( __('%s by %s
    %s'), date_i18n( 'm/d/y H:i', strtotime($note->date_created)), $note->added_by, $note->content ) . '
  • '; }} echo ''; }}` –  May 02 '21 at 04:52