Add Product Rows
Standard exports give you one row per product. Sometimes that’s not enough.
You might need to explode quantities into separate rows. Or treat shipping costs as products. Maybe add tax rows to your PDF.
Here’s how.
Split each quantity into its own row
By default, buying 5 chairs gives one row with qty=5. Your warehouse system might need 5 separate rows.
This code fixes that.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
// 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(); $line_id = 1; //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++) { if(isset($products[$pos]['line_id']) ) $products[$pos]['line_id']= $line_id++; $new_products[] = $products[$pos]; } } return $new_products; } , 10, 5); |
What it does: Takes a product with qty=5. Creates 5 identical rows. Each row shows qty=1.
When you need it: Barcode scanning. Warehouse picking lists. Commission calculations per unit.
Warning: Your export file gets much larger. 100 orders with 10 items each becomes 1000 rows. Test first.
Export shipping costs as product rows
Shipping isn’t a product. But your accounting system might want it in the product table.
|
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 |
//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); |
What you get: Shipping becomes a product line. SKU shows the shipping method ID. Price shows shipping cost.
Real example: Order has “Express Shipping – 15″.Exportshowsproductrownamed“ExpressShipping“withprice15.
Export fees as product rows
Gift wrapping. Handling fees. Payment charges. Turn them into product lines.
|
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 |
//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); |
Pro tip: This captures fee amounts even if your export normally ignores fees.
Export tax rows as products
Need to show tax breakdowns as separate lines?
|
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 |
//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); |
Result: Each tax type becomes a product row. SKU shows tax code. Price shows tax amount.
Add summary row to PDF reports
This one’s different. It adds a total row at the bottom of your PDF.
|
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(); |
Customize it: Change $summary_cols = array(9,10,11) to match your column numbers. Column 0 is the first column.
Styling: Yellow background. Black text. Size 14. Edit RGB values for different colors.
Where to put these snippets
Add to your theme’s functions.php. Use one snippet at a time. They can conflict.
Advanced Order Export for WooCommerce handles the base export. These snippets modify how product rows appear.
Common mistake
Running multiple row-modifying snippets together. They stack. You might get shipping rows AND duplicated quantities AND tax rows.
Test each snippet alone. Add them one by one.