Search code examples
wordpresswoocommercebackendhook-woocommerceorders

Show order notes with author and date on WooCommerce admin orders list


Using "Display back Order Notes in Admin orders list on WooCommerce 3.3" answer code, I am able to add Order notes column in Admin orders list, but it only shows that status changed from One status to another.

Now I would like also to show author of this change and date when it's happened, just, just like in order edit pages.

Any suggestions?


Solution

  • It is not necessary to use global $post, $the_order; with manage_shop_order_posts_custom_column. This is because there is a 2nd parameter that contains the $post_id


    $latest_note contains more then only the note, also the author and the date are available among others


    According to the rules of the art, add CSS via a stylesheet, not via admin_head


    It is always nice to see if you have found code and you want to add extra functionality, you first try before asking for help

    To answer your question, apply the following

    function custom_shop_order_column( $columns ) {
        $ordered_columns = array();
    
        foreach( $columns as $key => $column ){
            $ordered_columns[$key] = $column;
            if( 'order_date' == $key ){
                $ordered_columns['order_notes'] = __( 'Notes', 'woocommerce');
            }
        }
    
        return $ordered_columns;
    }
    add_filter( 'manage_edit-shop_order_columns', 'custom_shop_order_column', 10, 1 );
    
    
    function custom_shop_order_list_column_content( $column, $post_id ) {
    
        // Get $order object
        $order = wc_get_order( $post_id );
    
        if ( $column == 'order_notes' ) {
    
            if ( $order->get_customer_note() ) {
                echo '<span class="note-on customer tips" data-tip="' . wc_sanitize_tooltip( $order->get_customer_note() ) . '">' . __( 'Yes', 'woocommerce' ) . '</span>';
            }
    
            // Retrieves the amount of comments a post has.
            $amount_of_comments = get_comments_number( $post_id );
    
            if ( $amount_of_comments > 0 ) {
    
                $latest_notes = wc_get_order_notes( array(
                    'order_id' => $post_id,
                    'limit'    => 1,
                    'orderby'  => 'date_created_gmt',
                ) );
    
                $latest_note = current( $latest_notes );
    
                // Content
                $content = $latest_note->content;
    
                // Added by
                $added_by = $latest_note->added_by;
    
                // Date created - https://www.php.net/manual/en/function.date.php
                $date_created = $latest_note->date_created->date('j F Y - g:i:s');
    
                if ( isset( $content ) && $amount_of_comments == 1 ) {
                    echo '<span class="note-on tips" data-tip="' . wc_sanitize_tooltip( 'Author: ' . $added_by . '<br/>' . 'Date: ' . $date_created . '<br/>' . $content ) . '">' . __( 'Yes', 'woocommerce' ) . '</span>';
                } elseif ( isset( $content ) ) {
                    // translators: %d: notes count
                    echo '<span class="note-on tips" data-tip="' . wc_sanitize_tooltip( 'Author: ' . $added_by . '<br/>' . 'Date: ' . $date_created . '<br/>' . $content . '<br/><small style="display:block">' . sprintf( _n( 'Plus %d other note', 'Plus %d other notes', ( $amount_of_comments - 1 ), 'woocommerce' ), $amount_of_comments - 1 ) . '</small>' ) . '">' . __( 'Yes', 'woocommerce' ) . '</span>';
                } else {
                    // translators: %d: notes count
                    echo '<span class="note-on tips" data-tip="' . wc_sanitize_tooltip( sprintf( _n( '%d note', '%d notes', $amount_of_comments, 'woocommerce' ), $amount_of_comments ) ) . '">' . __( 'Yes', 'woocommerce' ) . '</span>';
                }
            }
        }
    }
    add_action( 'manage_shop_order_posts_custom_column' , 'custom_shop_order_list_column_content', 10, 2 );