Search code examples
phpwordpresswoocommercebackendorders

Display private and customer admin notes in a custom column on WooCommerce admin orders list


Based on the answer by LoicTheAzec on Add Admin order notes sent to customer in WooCommerce My Account Orders list

This is my attempt to display private and customer admin notes in a custom column on WooCommerce admin orders list, wrapped in <details> and <summary> tags.

// 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 '<details><summary>Click to read notes</summary><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>';
            }

if( $note->private_note ) {
                echo '<li class="order_private_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></details>';
    }
}

However, while the customer notes are displayed, this is not the case for the private notes. Any help will be appreciated!


Solution

    • $note->private_note does not exist.
    • The use of global variables is also not necessary for your question.

    So to display both customer notes and private admin notes you can use:

    // Add a Header
    function filter_manage_edit_shop_order_columns( $columns ) {
        // Add new column
        $columns['order_notes'] = __( 'Order notes', 'woocommerce' );
    
        return $columns;
    }
    add_filter( 'manage_edit-shop_order_columns', 'filter_manage_edit_shop_order_columns', 10, 1 );
    
    // Populate the Column
    function action_manage_shop_order_posts_custom_column( $column, $post_id ) {
        // Compare
        if ( $column == 'order_notes' ) {
            // Get order notes
            $notes = wc_get_order_notes( array(
                'order_id' => $post_id,
                'order_by' => 'date_created',
                'order' => 'ASC',
            ));
            
            // Output when NOT empty 
            if ( ! empty( $notes ) ) {
                echo '<a href="#"><details><summary>Click to read notes</summary><ul>';
    
                // Loop trough notes
                foreach( $notes as $note ) {
                    // NOT added by system
                    if ( $note->added_by !== 'system' ) {
                        
                        // Customer note OR private note
                        $note->customer_note ? $class = 'order_customer_note' : $class = 'order_private_note';
                        
                        echo '<li class="' . $class . '">' . sprintf( __('%s <br> %s'),
                            date_i18n( 'm/d/y H:i', strtotime( $note->date_created ) ),
                            $note->content
                        )  . '</li>';
                    }
                }
                
                echo '</ul></details></a>';
            }
        }
    }
    add_action( 'manage_shop_order_posts_custom_column' , 'action_manage_shop_order_posts_custom_column', 10, 2 );
    
    // CSS styles
    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_action( 'admin_print_styles', 'add_order_notes_column_style' );