在 WooCommerce 管理员订单列表中显示带有作者和日期的订单备注

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

使用 "" 答案代码,我可以在管理员订单列表中添加订单备注列,但它只显示状态从一个状态更改为其他。

现在我还想显示此更改的作者和更改发生的日期,就像在订单编辑页面中一样。

有什么建议吗?

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

要回答您的问题,请应用以下内容

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 );