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

时间:2020-04-11 08:41:51

标签: wordpress woocommerce hook-woocommerce

使用Display back Order Notes in Admin orders list on WooCommerce 3.3应答代码,我可以在“管理订单”列表中添加“订单备注”列,但它仅显示状态从一种状态更改为另一种状态。

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

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

没有必要将global $post, $the_order;manage_shop_order_posts_custom_column一起使用。这是因为第二个参数包含$post_id


$latest_note仅包含注释,还包含作者和日期


根据现有规则,请通过样式表而非CSS来添加admin_head


很高兴看到您是否已找到代码并想要添加其他功能, 您首先尝试先寻求帮助

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

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