Add Product Rows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
// duplicate each item "qty" times! and set qty=1 for the item add_filter( "woe_fetch_order_products", function ($products, $order, $labels, $format, $static_vals) { $new_products = array(); //need to know counters, as field "qty" can be omitted in output! foreach($order->get_items('line_item') as $pos=>$item) { if( !isset($products[$pos]) ) continue; if( isset($products[$pos]['qty']) ) $products[$pos]['qty'] =1; for($i=0;$i< $item['qty'];$i++) $new_products[] = $products[$pos]; } return $new_products; } , 10, 5); |
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 |
//export Shipping line as Products add_filter('woe_fetch_order_products', function ($products,$order,$labels, $format, $static_vals) { $i = count ($products); foreach ( $order->get_items('shipping') as $item_id=>$item ) { $row = array(); $i++; $taxes = $item->get_total_tax(); foreach ( $labels as $field => $label ) { if ( $field == 'line_id' ) { $row[ $field ] = $i; } elseif ( $field == 'sku' ) { $row[ $field ] = $item["method_id"]; } elseif ( $field == 'name' ) { $row['name'] = $item["name"]; } elseif ( $field == 'qty' ) { $row['qty'] = 1; } elseif ( $field == 'item_price' ) { $row['item_price'] = $item["cost"]; } elseif ( $field == 'price' ) { $row['price'] = $item["cost"]; } elseif ( $field == 'line_tax' ) { $row['line_tax'] = $taxes; } elseif ( $field == 'tax_rate' ) { $row['tax_rate'] = $item["cost"] ? round($taxes/$item["cost"] *100 , 2) : 0; } elseif ( isset( $static_vals[ $field ] ) ) { $row[ $field ] = $static_vals[ $field ]; } } $products[] = $row; } return $products; }, 10, 5); |
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 |
//export Fee line as Products add_filter('woe_fetch_order_products', function ($products, $order, $labels, $format, $static_vals) { $i = count($products); foreach($order-> get_items('fee') as $item_id => $item) { $item_meta = $order->get_item_meta( $item_id ); $fee_amount = $item_meta['_fee_amount'][0]; $tax_amount = $item_meta['_line_tax'][0]; $row = array(); $i++; foreach($labels as $field => $label) { if ($field == 'line_id') { $row[$field] = $i; } elseif($field == 'name') { $row['name'] = $item["name"]; } elseif($field == 'qty') { $row['qty'] = 1; } elseif($field == 'tax_rate') { $row[$field] = round($tax_amount/$fee_amount*100); } elseif($field == 'line_no_tax') { $row[$field] = $fee_amount; } elseif($field == 'line_tax') { $row[$field] = $tax_amount; } elseif($field == 'line_subtotal') { $row[$field] = $fee_amount; } elseif($field == 'line_total') { $row[$field] = $fee_amount; } elseif($field == 'line_total_plus_tax') { $row[$field] = $fee_amount + $tax_amount; } elseif($field == 'item_price') { $row[$field] = $fee_amount; } else { $row[$field] = $item[$field]; } } $products[] = $row; } return $products; }, 10, 5); |
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 |
//export Taxes as Product lines add_filter('woe_fetch_order_products', function ($products,$order,$labels, $format, $static_vals) { $i = count ($products); foreach ( $order->get_tax_totals() as $tax_code => $tax_total ) { $row = array(); $i++; foreach ( $labels as $field => $label ) { if ( $field == 'line_id' ) { $row[ $field ] = $i; } elseif ( $field == 'sku' ) { $row[$field] = $tax_code; } elseif ( $field == 'name' ) { $row[$field] = $tax_total->label; } elseif ( $field == 'item_price' ) { $row[$field] = wc_round_tax_total($tax_total->amount); } elseif ( $field == 'qty' ) { $row[$field] = 1; } elseif ( isset( $static_vals[ $field ] ) ) { $row[ $field ] = $static_vals[ $field ]; } } $products[] = $row; } return $products; }, 10, 5); |
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 |
//add summary row to PDF report class Woe_PDF_Summary { var $summary_cols = array(9,10,11); // columns starts from 0! var $num_columns = 11;// last column position var $totals; function __construct() { //init add_action( 'woe_pdf_started', function($pdf, $formatter){ $this->totals = array(); foreach($this->summary_cols as $pos) $this->totals[$pos] = 0; },10,2); //sum rows add_filter( 'woe_pdf_prepare_row', function($row){ foreach($this->summary_cols as $pos) $this->totals[$pos] += (float)$row[$pos]; return $row; },10,1); //add footer add_action("woe_pdf_finished", function($pdf, $formatter){ $row_style = array( 'size'=>14, 'text_color' => array(0,0,0), // black - RGB 'background_color' => array(255,255,0), // yellow - RGB ); $row = array("", "","", "Total","IDR" ); //just texts //add missed columns for($i=0; $i<$this->num_columns; $i++) $row[$i] = isset($row[$i]) ? $row[$i] : ""; //fill summary foreach($this->totals as $pos=>$val) $row[$pos] = $val; $pdf->addRow( $row, null, null, $row_style ); },10,2); } } new Woe_PDF_Summary(); |