Sort orders
Need orders sorted a specific way? By customer name? By product SKU?
Here’s how.
Sort products inside each order
By default, products appear in the order they were added. This sorts them alphabetically by SKU.
|
1 2 3 4 5 6 7 8 |
// sort products by SKU (inside one order) add_filter('woe_fetch_order_products', function ($products, $order, $labels, $format, $static_vals) { usort($products, function($a, $b){ if ($a['sku'] == $b['sku']) return 0; return ($a['sku'] < $b['sku']) ? -1 : 1; }); return $products; },10, 5); |
What it does: Takes all products in an order. Sorts them by SKU. A to Z.
Pro tip: Change 'sku' to 'name' to sort by product name instead.
When you need it: Warehouse picking lists. Barcode scanning. Any process that needs consistent product order.
Sort products in summary report
Same idea. For the summary report (grouped by product).
|
1 2 3 4 5 6 7 |
// sort products by SKU , summary report add_action( 'woe_summary_before_output', function() { uasort( $_SESSION['woe_summary_products'], function ( $a, $b ) { return strcmp( $a['sku'], $b['sku'] ); } ); }); |
What it does: Sorts your summary rows by SKU. Not random anymore.
Real example: Summary shows 50 products. Now they appear alphabetically. Easy to find specific items.
Sort orders by customer name (SQL method)
This one’s powerful. Sorts the actual orders before export.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// sort orders by billing full name using SQL filters add_filter('woe_sql_get_order_ids_left_joins', function ($joins) { global $wpdb; $joins[] = "LEFT JOIN {$wpdb->postmeta} AS billing_last ON (billing_last.post_id = orders.ID AND billing_last.meta_key='_billing_last_name')"; $joins[] = "LEFT JOIN {$wpdb->postmeta} AS billing_first ON (billing_first.post_id = orders.ID AND billing_first.meta_key='_billing_first_name')"; return $joins; }); add_filter('woe_sql_get_order_ids_fields', function ($fields) { return "ID AS order_id, CONCAT(billing_first.meta_value,billing_last.meta_value) AS billing_name"; }); add_filter('woe_sql_get_order_ids_order_by', function ($order_by) { return "ORDER BY billing_name"; }); |
What it does: Changes the database query. Orders come out sorted by customer full name.
Result: Smith, John appears before Taylor, Sarah. A to Z by last name.
Warning: This affects the entire export. Every order sorts by name. Not just products within orders.
Sort orders by username
Need orders sorted by customer login name? Use this.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
// sort by usernames using SQL filters add_filter('woe_sql_get_order_ids_left_joins',function ($joins) { global $wpdb; $joins[] = "LEFT JOIN {$wpdb->postmeta} AS order_usernames ON (order_usernames.post_id = orders.ID AND order_usernames.meta_key='_customer_user')"; $joins[] = "LEFT JOIN {$wpdb->users} AS users ON users.ID = order_usernames.meta_value"; return $joins; }); add_filter('woe_sql_get_order_ids_fields', function ($fields) { return "orders.ID AS order_id,users.user_login AS username"; }); add_filter('woe_sql_get_order_ids_order_by', function ($order_by) { return "ORDER BY username DESC"; }); |
What it does: Sorts by username. DESC means Z to A. Change to ASC for A to Z.
When you need it: VIP customer reports. Membership sites. Any store where username matters more than order number.
Which method should you use?
Inside orders: Use the first snippet (woe_fetch_order_products). Sorts products within each order.
Summary report: Use the second snippet (woe_summary_before_output). Sorts grouped products.
Order list: Use SQL filters (third or fourth snippet). Sorts which orders appear first.
Where to put these snippets
Add to your theme’s functions.php. Paste at the bottom.
Advanced Order Export for WooCommerce runs your export. These snippets change the sort order at different stages.
Common mistake
Using SQL filters with complex exports. If you have many joins already, adding more can slow things down.
Also, don’t combine multiple SQL sort filters. They’ll conflict. Pick one.
Pro tip
Want to sort by total order value? Change the SQL field to order_total. Pull from postmeta with key _order_total.
Want random order? Use ORDER BY RAND(). Great for sample exports.
Test with 10 orders first. Verify sort works. Then run full export.
Real talk
The built-in “Sort orders by” dropdown handles most cases. Use these snippets only when you need custom sorting logic. Like combining first and last names. Or sorting products inside orders.
Start simple. Add complexity only when needed.