Sort by product names
Need products sorted alphabetically in your export? Not by order. Not by SKU. By product name.
Here’s how.
Built-in sorting (the easy way)
Good news. Since version 3.2, Advanced Order Export for WooCommerce includes sorting options.
Go to your export profile. Look for “Sort orders by” dropdown. Choose your field. No code needed.
This works for PDF (outdated) and XLS(outdated) formats.
The code below is outdated. Use the dropdown instead.
Sort products inside PDF exports
This code sorts products by “Item Name” within each PDF order.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
// Sort by column "Item Name" // Format PDF class WOE_PDF_Sort{ function __construct() { add_action("woe_settings_above_buttons", array($this,"draw_options") ); add_filter("woe_settings_validate_defaults", function($settings) { if( !empty($settings[ 'sort_by_products' ]) ) { add_action("woe_pdf_started",array($this,"rebuild_pdf_file"),10,2); } return $settings; }); } // 1 function draw_options($settings) { $selected = !empty($settings[ 'sort_by_products' ]) ? 'checked': ''; echo '<input name="settings[sort_by_products]" type="hidden" value="0" /> <input name="settings[sort_by_products]" type="checkbox" value="1" /> <span class="wc-oe-header">Sort by products by field "Item Name", Format PDF</span>'; } //2 function rebuild_pdf_file($pdf, $formatter){ //read $file = fopen($formatter->filename, 'r'); $keys = fgetcsv($file); $keys[] = "__order_id"; $rows = array(); while ($val = fgetcsv($file)) { $row = array_combine($keys, $val); $order_id = $row["__order_id"]; if(!isset($rows[$order_id])) { //date as timestamp //$key = strtotime(str_replace(" om ", " , ", $row["Pickup Datum"])); $key = $row["Item Name"]; //EDIT this line $rows[$order_id] = array( "key"=>$key,"rows"=>array()); } $rows[$order_id]["rows"][]= $row; } fclose($file); //sort usort ($rows, function ($a, $b) { return $a['key']<$b['key'] ? -1:1; }); //write back $file = fopen($formatter->filename, 'w'); array_pop($keys); fputcsv($file,$keys); foreach($rows as $row) { foreach($row["rows"] as $rec) fputcsv($file,$rec); } fclose($file); } } new WOE_PDF_Sort(); |
How it works: Adds a checkbox to your settings. Check it. Products sort alphabetically by name within each order.
Pro tip: Change $key = $row["Item Name"] to $key = $row["SKU"] to sort by SKU instead.
Sort products inside XLS exports
Same idea. For Excel files.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
// Sort by product name // Format XLS // option "Fill order columns for" = ALL rows class Woe_Sort_By_Product_Name { function __construct() { //add settings add_action("woe_settings_above_buttons", array($this,"draw_options") ); // start SESSION add_filter("woe_settings_validate_defaults", function($settings) { if( !empty($settings[ 'sort_by_products' ]) ) { @session_start(); //stop default output for rows add_action("woe_xls_header_filter",array($this,"init_session_data"),10,2); add_action("woe_xls_output_filter",array($this,"record_xls_rows"),10,2); add_action("woe_xls_print_footer",array($this,"sort_rows"),10,2); } return $settings; }); } // 1 function draw_options($settings){ $selected = !empty($settings[ 'sort_by_products' ]) ? 'checked': ''; echo '<input type=hidden name="settings[sort_by_products]" value="0"> <input type=checkbox name="settings[sort_by_products]" value="1" '. $selected .'> <span class="wc-oe-header">Sort by products, Format XLS</span>'; } // 2 init storage function init_session_data($data) { $_SESSION['woe_temp_rows'] = array(); return $data; } //3 down't export rows function record_xls_rows($data,$obj) { $_SESSION['woe_temp_rows'][] = $data; return false; } //4 sort and print function sort_rows($phpExcel,$formatter) { usort($_SESSION['woe_temp_rows'], function($a,$b) { return $a['plain_products_name'] < $b['plain_products_name'] ? -1: 1; }); foreach($_SESSION['woe_temp_rows'] as $pos=>$data) $this->output_modified_xls($formatter,$data,$pos+2); } function output_modified_xls($formatter,$rec,$row) { $pos = 0; foreach ( $rec as $field => $text ) { $formatter->objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow( $pos, $row, $text ); $pos++; } } } new Woe_Sort_By_Product_Name(); |
Important setting: Go to Setup Fields. Set “Fill order columns for” to “ALL rows”. Otherwise sorting breaks.
Where to put these snippets
Add to your theme’s functions.php. Each snippet works independently.
Common mistake
Using outdated PDF/XLS sorting code. The dropdown in section “Sort orders by” is now the standard way.
Only use these code snippets if you need product-level sorting inside orders. Not order-level sorting.
Pro tip
Test on a small date range first. Verify products appear alphabetically. Check that orders still group correctly.
Want descending order (Z to A)? Change -1 : 1 to 1 : -1 in the usort line.
Real talk
The built-in dropdown handles 90% of sorting needs. Use that first.
These code snippets are for edge cases. Like sorting products within orders. Or very old plugin versions.
Update to the latest Advanced Order Export for WooCommerce. The dropdown is much simpler.