Sort by product names
Codes for formats : PDF (outdated) , XLS(outdated)
Since version 3.2 for PDF/XLS formats – you should use dropdown in section “Sort orders by”
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 |
// 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(); |
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(); |