【问题标题】:Show Order Notes with author and date in Woocommerce Admin Order List在 Woocommerce 管理订单列表中显示带有作者和日期的订单备注
【发布时间】:2020-04-11 08:41:51
【问题描述】:

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

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

有什么建议吗?

【问题讨论】:

    标签: wordpress woocommerce hook-woocommerce


    【解决方案1】:

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

    【讨论】:

    • 非常感谢,你帮了我很多
    • 是的,这可能是 $latest_notes (array) 当前包含的限制为 1,正如您在代码中看到的那样。如果您删除此限制,您将获得一个包含所有订单备注的数组。然后您可以使用 foreach 循环打印数组。
    猜你喜欢
    • 2018-08-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多