如何在WooCommerce订单列表表格中添加备注列并直接新增备注?
实现方案
完全可以实现订单列表页直接新增私人备注的需求,你只需要替换原有代码为下方完整代码,放在子主题的functions.php文件中即可生效:
// WooCommerce 订单列表新增可编辑私人备注列 // 保留Actions列为最后一列,展示最新私人备注,支持列表页直接新增 // 代码放置于子主题functions.php // 1. 新增"订单备注"列 add_filter( 'manage_edit-shop_order_columns', 'add_order_notes_column' ); function add_order_notes_column( $columns ) { $new_columns = is_array( $columns ) ? $columns : []; unset( $new_columns['wc_actions'] ); $new_columns['order_notes'] = '私人备注'; $new_columns['wc_actions'] = '操作'; return $new_columns; } // 2. 列样式添加 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 10px 18px; list-style-type: disc; }'; $css .= '.order_private_note { color: #0000ee; }'; $css .= '.order-note-input { width: 100%; margin: 8px 0; padding: 4px; }'; $css .= '.submit-note-btn { font-size: 12px; padding: 2px 8px; }'; wp_add_inline_style( 'woocommerce_admin_styles', $css ); } // 3. 列内容渲染:展示最新备注+新增表单 add_action( 'manage_shop_order_posts_custom_column', 'add_order_notes_content' ); function add_order_notes_content( $column ) { if( $column != 'order_notes' ) return; global $post, $the_order; if( empty( $the_order ) || $the_order->get_id() != $post->ID ) { $the_order = wc_get_order( $post->ID ); } $args = [ 'order_id' => $the_order->get_id(), 'order_by' => 'date_created', 'order' => 'DESC' ]; $notes = wc_get_order_notes( $args ); // 展示最新私人备注 if( $notes ) { echo '<ul class="note-list">'; foreach( $notes as $note ) { if( ! $note->customer_note ) { echo '<li class="order_private_note">'; $date = date( 'd/m/y H:i', strtotime( $note->date_created ) ); echo $date.' by '.$note->added_by.'<br>'.$note->content.'</li>'; break; } } echo '</ul>'; } // 新增备注表单 echo '<form class="add-order-note-form" data-order-id="'.esc_attr($the_order->get_id()).'">'; echo '<textarea class="order-note-input" rows="2" placeholder="输入新的私人备注"></textarea>'; echo '<button type="submit" class="button button-secondary submit-note-btn">保存备注</button>'; echo '</form>'; } // 4. 注入AJAX处理脚本 add_action( 'admin_enqueue_scripts', 'add_order_note_ajax_script' ); function add_order_note_ajax_script() { // 仅在订单列表页加载脚本 $screen = get_current_screen(); if( $screen->id !== 'edit-shop_order' ) return; ?> <script> jQuery(function($){ $('.add-order-note-form').on('submit', function(e){ e.preventDefault(); const $form = $(this); const orderId = $form.data('order-id'); const noteContent = $form.find('.order-note-input').val().trim(); if(!noteContent) return; $.post(ajaxurl, { action: 'add_private_order_note', order_id: orderId, note_content: noteContent, nonce: '<?php echo wp_create_nonce("add_private_note_".$screen->id); ?>' }, function(res){ if(res.success) { // 刷新当前列的备注展示 $form.siblings('.note-list').replaceWith(res.data.note_html); $form.find('.order-note-input').val(''); } else { alert('备注保存失败,请刷新后重试'); } }) }) }) </script> <?php } // 5. AJAX保存备注逻辑 add_action( 'wp_ajax_add_private_order_note', 'handle_add_private_order_note' ); function handle_add_private_order_note() { // 权限与参数校验 if(!current_user_can('manage_woocommerce_orders') || !isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'add_private_note_edit-shop_order')) { wp_send_json_error('权限不足'); } $order_id = intval($_POST['order_id']); $note_content = sanitize_textarea_field($_POST['note_content']); if(!$order_id || !$note_content) { wp_send_json_error('参数错误'); } // 保存私人备注 wc_create_order_note($order_id, $note_content, false, false); // 重新获取最新备注返回前端 $notes = wc_get_order_notes([ 'order_id' => $order_id, 'order_by' => 'date_created', 'order' => 'DESC' ]); $note_html = '<ul class="note-list">'; foreach($notes as $note) { if(!$note->customer_note) { $note_html .= '<li class="order_private_note">'; $date = date( 'd/m/y H:i', strtotime( $note->date_created ) ); $note_html .= $date.' by '.$note->added_by.'<br>'.$note->content.'</li>'; break; } } $note_html .= '</ul>'; wp_send_json_success(['note_html' => $note_html]); }
使用说明
- 代码生效后,订单列表的私人备注列会同时展示最新私人备注和输入框,填写内容后点击保存即可直接新增备注,无需进入订单详情页
- 所有备注均为仅后台可见的私人备注,不会发送给客户
- 内置权限校验,仅拥有订单管理权限的用户可以添加备注
内容的提问来源于stack exchange,提问作者Joe
相关产品推荐
相关产品推荐

