Alter summary report
The summary report groups products by SKU or name. But sometimes you need more. Sort differently. Add totals. Count orders.
Here’s how.
Sort products by SKU
By default, the summary report sorts randomly. This code sorts alphabetically by SKU.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//sort by sku add_action('woe_summary_before_output' , function() { uasort($_SESSION['woe_summary_products'], function($a,$b) { if( empty($a['sku']) AND empty($b['sku']) ) return 0; if( empty($a['sku']) ) return 1; if( empty($b['sku']) ) return -1; return strcmp($a['sku'],$b['sku']); }); }); |
What it does: Products without SKUs go to the bottom. The rest sort A to Z.
When you need it: Your warehouse picks by SKU order. Your accountant wants alphabetical reports.
Pro tip: Change strcmp($a['sku'],$b['sku']) to strcmp($a['name'],$b['name']) to sort by product name instead.
Add a total row at the bottom
Summary reports show individual product totals. But no grand total row. This adds one.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
//add Total row at bottom add_action('woe_summary_before_output' , function() { $row1 = reset($_SESSION['woe_summary_products']); $keys = array_keys($row1); $key1 = reset($keys); //init $total = array(); foreach($row1 as $k=>$v) $total[$k] = strpos($k,"summary_report_total")!==false ? 0 : ""; $total[$key1] = "Total"; //sum foreach($_SESSION['woe_summary_products'] as $k=>$data) { foreach($data as $k=>$v) if(strpos($k,"summary_report_total")!==false) $total[$k] +=$v; } //done $_SESSION['woe_summary_products'][] = array(""); //empty $_SESSION['woe_summary_products'][] = $total; }); |
What you get: A “Total” row at the bottom. All number columns summed up.
Real example: Report shows 10 products. Last row says “Total” with combined quantities and revenue.
Warning: Only columns with “summary_report_total” in their key get summed. Standard columns like quantity and total work fine. Custom columns might need adjustment.
Add a “Total Orders” column
Standard summary groups by product. Shows total quantity sold. But not how many orders contained that product.
This code adds that column.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// Add column "Total Orders" to "Summary by products" class WOE_Total_Orders_Summary{ function __construct() { add_filter('woe_summary_headers',function ($headers) { $headers[] = "Total Orders"; // just text return $headers; }); add_filter('woe_summary_column_keys',function ($cols) { $cols['count'] = 0; // new key with default value return $cols; }); add_action('woe_summary_products_add_item',function ($key, $item, $order) { $_SESSION['woe_summary_products'][$key]['count'] ++; // add 1 for each order },10,3); } } new WOE_Total_Orders_Summary(); |
What it does: Adds a column showing how many unique orders included each product.
Real example: Blue Widget sold 50 units across 30 orders. Red Gadget sold 45 units across 45 orders (one per order). The “Total Orders” column reveals that pattern.
When you need it: You sell high-value items. Want to know order frequency vs quantity. You analyze repeat purchases. Need to see if customers buy multiples.
Where to put these snippets
Add to your theme’s functions.php. Each snippet works independently. Use one or all three.
Advanced Order Export for WooCommerce generates the summary report. These snippets modify the output before it reaches your screen or CSV.
Common mistake
Placing snippets in wrong order. The “Total Orders” column code must run before the summary builds. That’s why it uses woe_summary_products_add_item action.
Also, the total row snippet adds two rows at the bottom (one empty, one total). Don’t remove the empty row. It creates visual separation.
Pro tip
Combine all three snippets. Sort by SKU. Add total orders column. Add grand total row. Clean, professional summary report every time.
Test on a small date range first. Verify your numbers add up. The total row should match manual calculations.