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

如何在WooCommerce订单页面添加客户历史订单数量列?

在WooCommerce订单列表添加客户总订单数列

要实现这个需求,你可以通过WooCommerce提供的两个核心钩子来完成,直接将以下代码添加到你的主题functions.php文件中,或者使用自定义功能插件:

1. 新增列标题

使用manage_edit-shop_order_columns钩子在订单列表头部添加新列的标题:

add_filter( 'manage_edit-shop_order_columns', 'add_customer_total_orders_column' );
function add_customer_total_orders_column( $columns ) {
    // 在"客户"列后插入新列
    $new_columns = [];
    foreach ( $columns as $key => $title ) {
        $new_columns[ $key ] = $title;
        if ( $key === 'customer' ) {
            $new_columns['customer_total_orders'] = '客户总订单数';
        }
    }
    return $new_columns;
}

2. 填充列内容

使用manage_shop_order_posts_custom_column钩子获取并显示对应客户的总订单数:

add_action( 'manage_shop_order_posts_custom_column', 'display_customer_total_orders_column_content' );
function display_customer_total_orders_column_content( $column ) {
    global $post;
    
    if ( $column === 'customer_total_orders' ) {
        $order = wc_get_order( $post->ID );
        $customer_id = $order->get_customer_id();
        
        if ( $customer_id ) {
            // 查询该客户的所有有效订单(排除草稿状态)
            $total_orders = wc_get_customer_order_count( $customer_id );
            echo esc_html( $total_orders );
        } else {
            echo '-'; // 访客订单显示横线
        }
    }
}

说明

  • 代码中使用wc_get_customer_order_count()函数直接获取客户的有效订单数,该函数默认统计已完成、处理中等非草稿状态的订单
  • 访客订单(无客户ID)会显示-,你可以根据需求修改这个显示内容
  • 如果需要调整列的位置,只需修改第一个函数中插入新列的位置判断逻辑

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 23:05:28