SKU by columns and CSV format
-
Hi,
I have found your documentation about exporting Products as columns :
https://docs.algolplus.com/algol_order_export/export-products-as-columns/I was wondering, is there a way to adapt this to :
– display SKU instead of product names
– have a CSV file instead of an XLS fileThank you.
-
Hello
to use SKU – you should replace
$product_name = $item['name'];
with$product = $order->get_product_from_item( $item ) $product_name = $product->get_sku();
for CSV — it’s quite complex .
I’m sorry, but I can’t program it now.
You can check following code and use it as start point.
This code removes empty columns.?@session_start(); add_filter( "woe_csv_header_filter", function( $headers) { $_SESSION['woe_rows']= array(); return $headers; }); //save all rows to array add_filter( "woe_csv_custom_output_func", function($custom_output,$handle,$data,$delimiter,$linebreak,$enclosure,$is_header) { $_SESSION['woe_rows'][] = array_values($data); return true; },10,7); add_action( "woe_csv_print_footer", function($handle, $formatter ){ $header = reset($_SESSION['woe_rows']); foreach($header as $col=>$name) { $col_empty = true; for($i=1;$i<count($_SESSION['woe_rows']);$i++) { if( !empty($_SESSION['woe_rows'][$i][$col])) $col_empty = false; } if($col_empty) for($i=0;$i<count($_SESSION['woe_rows']);$i++) unset($_SESSION['woe_rows'][$i][$col]); } //done foreach($_SESSION['woe_rows'] as $row) fputcsv($handle,$row); },10,2);
Thanks for “SKU in columns” solution, works great!
And thanks for the hint for CSV format, but I think it’s out of my knowledge ??here is code for CSV
1. add fields SKU and Quantity to “Setup Fields”
2. put following php code in section “Misc Settings”@session_start(); add_filter( "woe_csv_header_filter", function( $headers) { $_SESSION['woe_rows']= array(); return $headers; }); //save all rows to array add_filter( "woe_csv_custom_output_func", function($custom_output,$handle,$data,$delimiter,$linebreak,$enclosure,$is_header) { $_SESSION['woe_rows'][] = array_values($data); return true; },10,7); add_action( "woe_csv_print_footer", function($handle, $formatter ){ $header = reset($_SESSION['woe_rows']); $sku_pos = array_search("SKU",$header); $qty_pos = array_search("Quantity",$header); $sku_start_pos = count($header); if($sku_pos !== false) { $added_sku_pos = array();// must rember SKU=>col pairs for($i=1;$i<count($_SESSION['woe_rows']);$i++) { $sku = $_SESSION['woe_rows'][$i][$sku_pos]; if( $sku === "") $sku = "-empty-"; $qty = $_SESSION['woe_rows'][$i][$qty_pos]; if( !isset($added_sku_pos[$sku])) {// new sku $added_sku_pos[$sku] = count($header); $header[] = $sku; } $new_sku_pos = $added_sku_pos[$sku]; //add missed cells for($j=$sku_start_pos;$j<$new_sku_pos;$j++) $_SESSION['woe_rows'][$i][] = ""; $_SESSION['woe_rows'][$i][$new_sku_pos] = $qty; // set new value // delete unused columns unset($_SESSION['woe_rows'][$i][$sku_pos]); unset($_SESSION['woe_rows'][$i][$qty_pos]); } //prepare header $max_pos = count($header); unset($header[$sku_pos]); unset($header[$qty_pos]); fputcsv($handle,$header); for($i=1;$i<count($_SESSION['woe_rows']);$i++) { fputcsv($handle,$_SESSION['woe_rows'][$i]); } } },10,2);
-
This reply was modified 3 years, 4 months ago by
algol.plus.
I have created the 2 fields and dragged them on the left side, but it didn’t work.
I have “-empty-” column where I have dragged 2 fields SKU and Quantity.This is the header I see in the csv file :
Name,”Address 1″,”Address 2″,City,State,”Postal Code”,Country,Phone,Notes,-empty-Please, just drag them from section Setup Fields>Products(Product Order Items)
Now I can see my SKUs in the header.
But cells (for quantity) are empty.Also I now have multiple rows for a same order (because we added Products(Product Order Items) fields I guess).
ok, I see the problem.
I’ll provide updated version tomorrow .hello
Please, replace code with this version.
go to Export Now , mark checkbox at bottom .but ALL product/coupon fields will be ignored in Setup Fields!
// Export Products as columns // Format - CSV // Checked - Output column titles as first line class Woe_Product_Columns_CSV { function __construct() { @session_start(); //add settings, , skip products add_action("woe_settings_above_buttons", array($this,"draw_options") ); add_filter("woe_settings_validate_defaults",array($this,"modify_export"),10,1); } // 1 function draw_options($settings){ $selected = !empty($settings[ 'products_as_columns' ]) ? 'checked': ''; echo '<br><br> <input type=hidden name="settings[products_as_columns]" value="0"> <input type=checkbox name="settings[products_as_columns]" value="1" '. $selected .'> <span class="wc-oe-header">Export products as columns, print <select name="settings[products_as_columns_output_field]" style="width: 100px"> <option value="qty">Qty</option> <option value="line_total">Amount</option> </select> in cell</span><br>'; } function modify_export($current_job_settings) { if( empty($current_job_settings['products_as_columns']) ) return $current_job_settings; // remove products/coupons fields which might require 2+ rows per order foreach($current_job_settings["order_fields"] as $k=>$f){ if ( $f["segment"] == "products" OR $f["segment"] == "coupons" ) unset($current_job_settings["order_fields"][$k]); } //remember field to output in new cells $this->output_field = $current_job_settings['products_as_columns_output_field']; //save all rows to array add_filter( "woe_csv_custom_output_func", function($custom_output,$handle,$data,$delimiter,$linebreak,$enclosure,$is_header) { if($is_header) { $_SESSION['woe_rows']= array(); $_SESSION['woe_product_columns']= array(); $_SESSION['woe_rows'][] = $data; // return true; } $order = new WC_Order(WC_Order_Export_Engine::$order_id); $extra_cells = array_fill(0, count($_SESSION['woe_product_columns']), ""); // work with products foreach($order->get_items('line_item') as $item_id=>$item) { $product = $order->get_product_from_item( $item ); $column_name = $product->get_sku(); // we use SKU //$column_name = $item['name']; // uncomment to use "item name" as column if($column_name === "") $column_name = "-empty-"; $pos = array_search($column_name,$_SESSION['woe_product_columns']); if( $pos === false) { // new product detected $extra_cells[] = $item[ $this->output_field ]; $_SESSION['woe_rows'][0][] = $column_name;//modify header! $_SESSION['woe_product_columns'][] = $column_name; } else { $extra_cells[$pos] = $item[ $this->output_field ]; } } foreach($extra_cells as $pc) $data[] = $pc; $_SESSION['woe_rows'][] = $data; return true; },10,7); //output session data add_action("woe_csv_print_footer", function($handle, $formatter) { foreach($_SESSION['woe_rows'] as $row) fputcsv($handle,$row); unset($_SESSION['woe_rows']);//done },10,2); return $current_job_settings; } } new Woe_Product_Columns_CSV();
As usual, amazing work!
Huge help for us, thank you!so everything is ok ? any notes ?
-
This reply was modified 3 years, 4 months ago by
algol.plus.
yes, works well!
Ok, got it
-
This reply was modified 3 years, 4 months ago by
- The topic ‘SKU by columns and CSV format’ is closed to new replies.