在 WooCommerce 管理订单列表的自定义列中显示私人和客户管理注释

哈罗德·奥尔德森

基于 LoicTheAzec在 WooCommerce 我的帐户订单列表中发送给客户的添加管理员订单备注的回答

这是我尝试在 WooCommerce 管理订单列表的自定义列中显示私人和客户管理注释,包装在<details><summary>标签中。

// 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>';
    }
}

然而,虽然显示客户备注,但私人备注并非如此。任何帮助将不胜感激!

7uc1f3r
  • $note->private_note 不存在。
  • 您的问题也不需要使用全局变量。

因此,要同时显示客户备注和私人管理员备注,您可以使用:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在Woocommerce管理员订单列表自定义列中显示结帐字段值

在Woocommerce管理员订单列表自定义列中显示用户名

在Woocommerce管理订单列表中显示具有“全部”自定义状态的订单

仅在WooCommerce管理员订单列表自定义列中显示特定订单状态的数据

在 WooCommerce 管理订单列表的自定义列中显示每个订单项目的库存数量

单击以在WooCommerce管理订单列表中显示文本时隐藏自定义操作按钮

在Woocommerce管理订单列表中添加可排序的自定义列

自定义操作按钮进入WooCommerce管理订单列表上的自定义列

WooCommerce 管理订单列表中自定义订单状态的操作按钮问题

在Woocommerce管理员订单列表“订单”现有列中添加自定义字段

将img添加到Woocommerce的管理订单列表中的自定义操作按钮

在WooCommerce管理订单列表中添加自定义操作按钮

在Woocommerce中处理管理订单列表上的自定义批量操作

Woocommerce 3.3管理员订单列表中的自定义订单状态背景按钮颜色

在WooCommerce管理员订单列表中以批量操作添加自定义取消订单状态

在WooCommerce 3.3的管理订单列表中显示回购订单注释

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

将自定义ajax按钮添加到WooCommerce管理订单列表

在Woocommerce管理员订单列表上处理多个自定义批量操作

在WooCommerce中将自定义URL链接添加到管理订单列表页面

在Woocommerce 3中将自定义批量操作添加到管理订单列表

在 WooCommerce 订单列表中显示 Dokan 自定义订单元数据

在WooCommerce订单管理员列表上显示带有自定义用户元的自定义列

在Woocommerce管理员订单编辑页面中以编程方式添加自定义订单注释

将自定义订单状态添加到WooCommerce管理员订单列表中的过滤器菜单

在Woocommerce管理订单列表上的一列中显示用户简历

在WooCommerce管理员订单列表中显示每个订单的变体名称

在Woocommerce管理员,订单和电子邮件中显示自定义付款字段

保存结帐自定义字段值并将其显示在WooCommerce管理订单中