You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce后台订单列表自定义列私有管理员备注不显示求助

核心问题原因

WooCommerce 调用wc_get_order_notes()获取的备注对象不存在private_note属性,原有代码中判断私有备注的条件永远无法匹配,因此私有管理员备注不会输出。

修正逻辑说明
  • WooCommerce 对订单备注的属性定义仅通过customer_note布尔值区分可见性,无独立的私有备注字段
  • 管理员创建的私有备注判断规则:排除系统自动生成的日志类备注(即added_by !== 'system')、且customer_note值为false的条目,即为后台私有管理员备注
  • 配套优化:如果当前订单没有可展示的非系统备注,不渲染空的折叠容器,减少无意义UI占位;合并重复的渲染逻辑,降低代码冗余
修正后可直接运行的完整代码
// 后台订单列表新增自定义备注列
add_filter( 'manage_edit-shop_order_columns', 'add_order_notes_column' );
function add_order_notes_column( $columns ) {
    $columns['order_notes'] = 'Order Notes';
    return $columns;
}

// 列样式适配
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; }'; // 客户可见备注:红色
    $css .= '.order_private_note { color: #0000ee; }'; // 私有备注:蓝色
    wp_add_inline_style( 'woocommerce_admin_styles', $css );
}

// 自定义列内容渲染
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( [
        'order_id' => $order->get_id(),
        'order_by' => 'date_created',
        'order'    => 'ASC',
    ] );

    $render_content = '';
    foreach( $notes as $note ) {
        // 跳过系统自动生成的状态变更、支付日志类备注
        if( 'system' === $note->added_by ) continue;

        $note_class = $note->customer_note ? 'order_customer_note' : 'order_private_note';
        $render_content .= '<li class="' . $note_class . '">' . sprintf( __('%s by %s <br> %s:'),
            date_i18n( 'm/d/y H:i', strtotime( $note->date_created ) ),
            $note->added_by,
            $note->content
        ) . '</li>';
    }

    // 仅当有可展示的备注内容时,才渲染折叠容器
    if( ! empty( $render_content ) ) {
        echo '<details><summary>Click to read notes</summary><ul>' . $render_content . '</ul></details>';
    }
}

以上代码完全兼容 WooCommerce 自定义订单表(HPOS)功能,无需额外适配修改。


内容的提问来源于stack exchange,提问作者user14906105

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.22 11:15:04