如何使用$wc_report->get_order_report_data实现WooCommerce多月产品销售报表
核心调整思路
WC_Admin_Report的时间范围筛选完全依赖实例上的start_date、end_date属性,你之前的问题是初始化实例后就把这两个值固定成当月范围,后续所有查询都复用了这个固定值。实际上这两个属性是可以动态修改的——每次执行查询前,把属性值改成目标统计区间的时间戳,就能正常拿到对应区间的查询结果,不需要重新实例化类,也不需要改动类的内部逻辑。
分步实现逻辑
1. 抽出通用查询方法
把原来的重复查询逻辑抽成独立函数,传不同的时间范围就能返回对应区间的销量映射,避免冗余代码:
/** * 获取指定时间区间内的产品销量映射 * @param int $start_date 区间开始时间戳 * @param int $end_date 区间结束时间戳 * @param WC_Admin_Report $wc_report 已初始化的报告实例 * @return array 键为product_id,值为对应总销量 */ function get_sales_map_for_range($start_date, $end_date, $wc_report) { // 临时修改实例的时间范围属性 $wc_report->start_date = $start_date; $wc_report->end_date = $end_date; $results = $wc_report->get_order_report_data(array( 'data' => array( '_product_id' => array( 'type' => 'order_item_meta', 'order_item_type' => 'line_item', 'function' => '', 'name' => 'product_id' ), '_qty' => array( 'type' => 'order_item_meta', 'order_item_type' => 'line_item', 'function' => 'SUM', 'name' => 'quantity' ) ), 'query_type' => 'get_results', 'group_by' => 'product_id', 'order_types' => wc_get_order_types('order_count'), 'filter_range' => TRUE, 'order_status' => array('completed','processing','on-hold'), )); // 整理成关联映射,方便后续快速取值 $sales_map = []; foreach ($results as $row) { $sales_map[intval($row->product_id)] = intval($row->quantity); } return $sales_map; }
2. 提前计算所有统计维度的准确时间边界
一次性算好当月、上月、2个月前、3个月前的时间范围,和原有代码的时区逻辑保持一致,替代原来用固定86400秒计算月末的写法,避免夏令时切换导致的时间偏差:
global $woocommerce, $wpdb; include_once($woocommerce->plugin_path() . '/includes/admin/reports/class-wc-admin-report.php'); $wc_report = new WC_Admin_Report(); $wpdb->query('SET SQL_BIG_SELECTS=1'); // 基准时间:当月第一天0点,和原有逻辑保持一致 $current_month_start = strtotime(date('Y-m', current_time('timestamp')) . '-01 midnight'); // 4个统计维度的时间范围 $time_ranges = [ 'current' => [ 'start' => $current_month_start, 'end' => strtotime('+1 month', $current_month_start) - 1 // 当月最后一秒 ], 'last_1' => [ 'start' => strtotime('-1 month', $current_month_start), 'end' => $current_month_start - 1 // 上月最后一秒 ], 'last_2' => [ 'start' => strtotime('-2 months', $current_month_start), 'end' => strtotime('-1 month', $current_month_start) - 1 ], 'last_3' => [ 'start' => strtotime('-3 months', $current_month_start), 'end' => strtotime('-2 months', $current_month_start) - 1 ], ];
3. 批量预查询所有区间的销量数据
不要在前端产品循环里逐产品发起查询,提前把4个区间的销量全部拉取下来,总共仅需4次全量查询,比循环中逐产品查的方案性能提升数倍:
$current_sales = get_sales_map_for_range($time_ranges['current']['start'], $time_ranges['current']['end'], $wc_report); $last1_sales = get_sales_map_for_range($time_ranges['last_1']['start'], $time_ranges['last_1']['end'], $wc_report); $last2_sales = get_sales_map_for_range($time_ranges['last_2']['start'], $time_ranges['last_2']['end'], $wc_report); $last3_sales = get_sales_map_for_range($time_ranges['last_3']['start'], $time_ranges['last_3']['end'], $wc_report); // 按当月销量倒序整理产品列表,和原有排序逻辑一致 $sold_products = []; foreach ($current_sales as $product_id => $qty) { $sold_products[] = (object)[ 'product_id' => $product_id, 'quantity' => $qty ]; } usort($sold_products, function($a, $b) { return $b->quantity <=> $a->quantity; });
4. 前端表格渲染时直接匹配取值
把原来表格里的待填充位置替换为从对应销量映射数组取数,加空值兜底,没有历史销量的产品自动显示0:
<div> <table> <thead> <tr> <th><strong><?php _e('Name', 'text-domain') ?></strong></th> <th><strong><?php _e('Current Month', 'text-domain') ?></strong></th> <th><strong><?php _e('Last Month', 'text-domain') ?></strong></th> <th><strong><?php _e('2 Months ago', 'text-domain') ?></strong></th> <th><strong><?php _e('3 Months ago', 'text-domain') ?></strong></th> </tr> </thead> <tbody> <?php foreach ($sold_products as $product) { $pid = intval($product->product_id); ?> <tr> <td> <a href="<?php echo esc_url(get_edit_post_link($pid)); ?>"> <strong><?php echo html_entity_decode(get_the_title($pid)); ?></strong> </a> </td> <td><?php echo intval($current_sales[$pid] ?? 0); ?></td> <td><?php echo intval($last1_sales[$pid] ?? 0); ?></td> <td><?php echo intval($last2_sales[$pid] ?? 0); ?></td> <td><?php echo intval($last3_sales[$pid] ?? 0); ?></td> </tr> <?php } ?> </tbody> </table> </div>
避坑提示
- 原代码里用
strtotime('+1month', $start_date) - 86400计算月末时间的写法,在夏令时切换的月份会出现1小时的筛选偏差,换成取下月1号前1秒的计算方式,能保证订单统计的准确性。 - 渲染时一定要加空值兜底,没有历史销量的产品对应的数组键不存在,直接取值会触发PHP警告,用
?? 0的写法就能自动补0。 - 如果需要把近3个月有销量、但当月没销售的产品也纳入列表,只需要在整理
$sold_products时,合并四个区间的所有产品ID,再按当月销量倒序排列即可,不需要改动查询逻辑。
内容的提问来源于stack exchange,提问作者southafricanrob
相关产品推荐
相关产品推荐

