hi
I’m sorry for long function, it’s for CSV.
Sorting by any field + product summary for any formats will be added in next major (2.0) version only
// Sort by Product Title + Summary, see requirements below!
// Format - CSV
// Checked - Output column titles as first line
// Checked - Populate other columns if products exported as rows
// Button - Export w/o Progressbar
class Woe_Product_Summary {
function __construct() {
$this->header = false;
$this->lines = array();
add_action("woe_order_export_started",array($this,"start_new_order"),10,1);
add_filter("woe_get_order_product_item",array($this,"record_product_qty"),10,1);
add_filter("woe_get_order_product",array($this,"record_product_title"),10,1);
add_filter("woe_csv_custom_output_func",array($this,"remember_lines"),10,6);
add_action("woe_csv_print_footer",array($this,"print_ordered_list_and_summary"),10,1);
}
// 3 tricky functions, as we don't have product fields in v1.3.0
function start_new_order($order_id) {
$this->product_name = array();
$this->product_qty = array();
$this->product_pos = 0;
}
function record_product_qty($item) {
$this->item_qty = $item['qty'];
}
function record_product_title($product) {
$this->product_name[] = $product->post->post_title;
$this->product_qty[] = $this->item_qty;
return $product;
}
function remember_lines($continue,$handle, $data, $delimiter, $linebreak, $enclosure) {
if(! $this->header )
$this->header = $data;
else {
$name = $this->product_name[$this->product_pos];
$qty = $this->product_qty[$this->product_pos++];
if($name)
$this->lines[] = array('name'=>$name, 'qty'=>$qty, 'data'=>$data);
}
return true; // don't output!
}
function print_ordered_list_and_summary($handle) {
//sort lines
usort($this->lines, function ($a, $b) {
return strcmp($a["name"], $b["name"]);
});
// output sorted
fputcsv($handle,$this->header);
foreach($this->lines as $line) {
fputcsv($handle,$line['data']);
@$counters[$line['name']]+=$line['qty'];
}
//empty space
fwrite($handle,"\n\n\n");
// print summary
fputcsv($handle,array("Product Name","Total") );
foreach($counters as $name=>$cnt) {
fputcsv($handle,array($name,$cnt));
}
}
}
new Woe_Product_Summary();
-
This reply was modified 7 years, 6 months ago by algol.plus.