WooCommerce按订单状态及自定义ACF字段统计订单数开发需求
解决方案:按客户经理+订单状态统计并展示订单数
Hey there! Looks like you're building an internal WooCommerce plugin to track order counts split by both account managers and order statuses, with a table view showing each manager's pending, cancelled, and other status counts. Your current code snippet is a basic shortcode framework, but it doesn't handle grouping by managers or multi-status stats. Let's fix that with a complete implementation:
完整实现代码
function display_woocommerce_order_count_by_manager( $atts, $content = null ) { // 设置短码默认参数:默认统计常用订单状态 $args = shortcode_atts( array( 'statuses' => 'pending,on-hold,cancelled,completed', ), $atts ); // 拆分并整理目标状态列表 $target_statuses = array_map( 'trim', explode( ',', $args['statuses'] ) ); // 获取所有客户经理(假设用"account_manager"用户角色标识,可根据实际调整) $account_managers = get_users( array( 'role' => 'account_manager', 'orderby' => 'display_name', 'order' => 'ASC', ) ); // 初始化统计数组:每个客户经理对应各状态的订单数默认设为0 $order_stats = array(); foreach ( $account_managers as $manager ) { $order_stats[ $manager->ID ] = array( 'name' => $manager->display_name, 'counts' => array_fill_keys( $target_statuses, 0 ), ); } // 批量查询各状态订单,按客户经理分组统计数量 foreach ( $target_statuses as $status ) { $orders = wc_get_orders( array( 'status' => $status, 'limit' => -1, 'meta_query' => array( array( 'key' => '_account_manager', // 假设订单元字段存储客户经理ID,需根据实际业务调整 'compare' => 'EXISTS', ), ), ) ); foreach ( $orders as $order ) { $manager_id = $order->get_meta( '_account_manager' ); if ( isset( $order_stats[ $manager_id ] ) ) { $order_stats[ $manager_id ]['counts'][ $status ]++; } } } // 生成HTML表格输出 ob_start(); ?> <table class="order-manager-stats" style="width:100%; border-collapse: collapse; margin: 20px 0;"> <thead> <tr style="background:#f0f0f0;"> <th style="border:1px solid #ddd; padding:8px;">客户经理</th> <?php foreach ( $target_statuses as $status ) : ?> <th style="border:1px solid #ddd; padding:8px; text-transform: capitalize;"><?php echo str_replace( '-', ' ', $status ); ?>订单数</th> <?php endforeach; ?> </tr> </thead> <tbody> <?php foreach ( $order_stats as $stats ) : ?> <tr> <td style="border:1px solid #ddd; padding:8px;"><?php echo esc_html( $stats['name'] ); ?></td> <?php foreach ( $stats['counts'] as $count ) : ?> <td style="border:1px solid #ddd; padding:8px; text-align:center;"><?php echo esc_html( $count ); ?></td> <?php endforeach; ?> </tr> <?php endforeach; ?> </tbody> </table> <?php return ob_get_clean(); } add_shortcode( 'order_count_by_manager', 'display_woocommerce_order_count_by_manager' );
关键细节说明
- 自定义统计状态:你可以通过短码参数指定要统计的订单状态,比如只统计待处理和已取消订单:
[order_count_by_manager statuses="pending,cancelled"] - 客户经理适配:代码默认用
account_manager用户角色筛选客户经理,如果你的系统是通过订单自定义字段或其他方式关联客户经理,只需调整get_users查询或订单元字段的key即可。 - 性能优化:使用
wc_get_orders批量查询订单,避免逐个查询的性能损耗;如果订单量极大,还可以添加日期范围参数进一步缩小查询范围。 - 样式自定义:内置了基础表格样式,你可以修改
style属性或添加自定义类名,适配内部系统的UI风格。
使用方法
把这段代码添加到你的插件主文件或者当前主题的functions.php中,然后在WordPress后台的页面/文章里插入短码[order_count_by_manager],就能看到按客户经理和状态拆分的订单统计表格了。
内容的提问来源于stack exchange,提问作者Julia Galden
相关产品推荐
相关产品推荐

