• Resolved samrot

    (@samrot)


    Hi!

    We implemented the code “Export Products as columns” (Format – CSV) and we are using it to export clients as rows, products as columns and quantities (per client per product) in the middle.

    1. How can we implement the same but clients as columns and products as rows instead?
    2. How can we add a column/row that summarizes the total quantity of each product? When we add a ‘Products total’ or ‘Items total’ field as it is right now it adds a column that summarizes the total number of items per client and we’d like to get the total number of each product among all clients instead.

    Thank you!

    • This topic was modified 10 months, 3 weeks ago by samrot.
Viewing 15 replies - 1 through 15 (of 15 total)
  • Plugin Author algol.plus

    (@algolplus)

    2. Do you use code https://docs.algolplus.com/algol_order_export/export-products-as-columns/ ?

    please replace last function with this version

    //4
    function analyze_products_add_columns($phpExcel,$formatter) {
        // add products as titles
        foreach($this->product_columns as $pos=>$text){
            $formatter->objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow( $pos+$this->headers_added, 1, $text );
            //add formulas for total
            $L = PHPExcel_Cell::stringFromColumnIndex($pos + $this->headers_added);
            $row = $formatter->last_row;
            $formatter->objPHPExcel->getActiveSheet()->setCellValue( $L. ($row+1), "=SUM({$L}2:{$L}$row)");
        }
        //make first bold
        $last_column = $formatter->objPHPExcel->getActiveSheet()->getHighestDataColumn();
        $formatter->objPHPExcel->getActiveSheet()->getStyle( "A1:" . $last_column . "1" )->getFont()->setBold( true );
    }
    Plugin Author algol.plus

    (@algolplus)

    1. How can we implement the same but clients as columns and products as rows instead?

    Please, mark checkbox ‘Summary report by products’ and add this code to “Misc Settings”

    //Record customers for product
    add_action( "woe_summary_products_add_item", function ($key, $product_item, $order, $item ) {
    //gather names
    $name = $order->get_billing_first_name() . " " . $order->get_billing_last_name();
    if( empty($_SESSION['woe_summary_products'][$key]['customer-names']) )
    $_SESSION['woe_summary_products'][$key]['customer-names'] = array();
    //and qty
    $_SESSION['woe_summary_products'][$key]['customer-names'][] = $name;
    if( empty($_SESSION['woe_summary_products'][$key][$name]) )
    $_SESSION['woe_summary_products'][$key][$name] = 0;
    $_SESSION['woe_summary_products'][$key][$name] += $product_item['qty'];
    },10,4);

    //Rebuild product summary rows
    add_action( 'woe_summary_before_output', function(){
    global $woe_summary_products_headers_extra;
    //get customers
    $names = array();
    foreach($_SESSION['woe_summary_products'] as $data){
    $names = array_merge($names, $data['customer-names']);
    }
    $names = array_unique($names);
    sort($names);
    $woe_summary_products_headers_extra = $names;

    //rebuild rows
    foreach($_SESSION['woe_summary_products'] as $key=>$data){
    $new_row = $data;
    foreach($names as $name) {
    if( !isset($new_row[$name]) )//no data
    $new_row[$name.'-qty'] = "";
    else {//move to correct column
    $new_row[$name.'-qty'] = $new_row[$name];
    unset($new_row[$name]);
    }
    }
    unset($new_row['customer-names']);
    $_SESSION['woe_summary_products'][$key] = $new_row;
    }
    });

    //Add customers to header
    add_filter('woe_xls_header_filter_final', function($row){
    global $woe_summary_products_headers_extra;
    $row = array_merge($row, $woe_summary_products_headers_extra);
    return $row;
    });

    Thread Starter samrot

    (@samrot)

    Hi! Thank you for your reply.

    1. How can we implement the same but clients as columns and products as rows instead?

    Please, mark checkbox ‘Summary report by products’ and add this code to “Misc Settings”

    How can we make the ‘Summary Report Total Quantity’ column to display last, after the clients columns?

    2. How can we add a column/row that summarizes the total quantity of each product?

    Do you use code https://docs.algolplus.com/algol_order_export/export-products-as-columns/ ?

    please replace last function with this version

    How can we add a Title “Total” to that row?

    As I mentioned we are using the CSV format code, this one…

    // 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();

    How could we make it work with CSV format?

    Thanks!

    Plugin Author algol.plus

    (@algolplus)

    1. How can we make the ‘Summary Report Total Quantity’ column to display last, after the clients columns?

    //Record customers for product
    add_action( "woe_summary_products_add_item", function ($key, $product_item, $order, $item ) {
    //gather names
    $name = $order->get_billing_first_name() . " " . $order->get_billing_last_name();
    if( empty($_SESSION['woe_summary_products'][$key]['customer-names']) )
    $_SESSION['woe_summary_products'][$key]['customer-names'] = array();
    //and qty
    $_SESSION['woe_summary_products'][$key]['customer-names'][] = $name;
    if( empty($_SESSION['woe_summary_products'][$key][$name]) )
    $_SESSION['woe_summary_products'][$key][$name] = 0;
    $_SESSION['woe_summary_products'][$key][$name] += $product_item['qty'];
    },10,4);

    //Rebuild product summary rows
    add_action( 'woe_summary_before_output', function(){
    global $woe_summary_products_headers_extra;
    //get customers
    $names = array();
    foreach($_SESSION['woe_summary_products'] as $data){
    $names = array_merge($names, $data['customer-names']);
    }
    $names = array_unique($names);
    sort($names);
    $woe_summary_products_headers_extra = $names;

    //rebuild rows
    foreach($_SESSION['woe_summary_products'] as $key=>$data){
    $new_row = $data;
    foreach($names as $name) {
    if( !isset($new_row[$name]) )//no data
    $new_row[$name.'-qty'] = "";
    else {//move to correct column
    $new_row[$name.'-qty'] = $new_row[$name];
    unset($new_row[$name]);
    }
    }
    unset($new_row['customer-names']);
    //move field summary_report_total_qty at end $new_row
    $value = $new_row['summary_report_total_qty'];
    unset($new_row['summary_report_total_qty']);
    $new_row['summary_report_total_qty'] = $value;
    $_SESSION['woe_summary_products'][$key] = $new_row;
    }
    });

    //Add customers to header
    add_filter('woe_xls_header_filter_final', function($row){
    global $woe_summary_products_headers_extra;
    $pos = array_search("Summary Report Total Quantity", $row);
    if($pos!==false) unset($row[$pos]);
    $row = array_merge($row, $woe_summary_products_headers_extra,["Summary Report Total Quantity"]);
    return $row;
    });

    Plugin Author algol.plus

    (@algolplus)

    2. I updated 2nd code (for CSV) at page https://docs.algolplus.com/algol_order_export/export-products-as-columns/

    You must uncomment line 82!

    Thread Starter samrot

    (@samrot)

    It’s all working great so far…

    The only thing I’m missing is how to make the clients to display as columns and the ‘Summary Report Total Quantity’ column to display last in CSV format instead of XLS.

    Plugin Author algol.plus

    (@algolplus)

    1. How can we implement the same but clients as columns and products as rows instead?
      Please, mark checkbox ‘Summary report by products’ and add this code to “Misc Settings”.
      version for CSV format
    //Version for CSV !

    //Save header to array , don't print now !
    add_filter( "woe_csv_header_filter", function($data ){
    $_SESSION['woe_summary_products_header'] = $data;
    return [];
    });

    //Record customers for product
    add_action( "woe_summary_products_add_item", function ($key, $product_item, $order, $item ) {
    //gather names
    $name = $order->get_billing_first_name() . " " . $order->get_billing_last_name();
    if( empty($_SESSION['woe_summary_products'][$key]['customer-names']) )
    $_SESSION['woe_summary_products'][$key]['customer-names'] = array();
    //and qty
    $_SESSION['woe_summary_products'][$key]['customer-names'][] = $name;
    if( empty($_SESSION['woe_summary_products'][$key][$name]) )
    $_SESSION['woe_summary_products'][$key][$name] = 0;
    $_SESSION['woe_summary_products'][$key][$name] += $product_item['qty'];
    },10,4);

    //Rebuild product summary rows
    add_action( 'woe_summary_before_output', function(){
    //get customers
    $names = array();
    foreach($_SESSION['woe_summary_products'] as $data){
    $names = array_merge($names, $data['customer-names']);
    }
    $names = array_unique($names);
    sort($names);
    $woe_summary_products_headers_extra = $names;

    //rebuild rows
    foreach($_SESSION['woe_summary_products'] as $key=>$data){
    $new_row = $data;
    foreach($names as $name) {
    if( !isset($new_row[$name]) )//no data
    $new_row[$name.'-qty'] = "";
    else {//move to correct column
    $new_row[$name.'-qty'] = $new_row[$name];
    unset($new_row[$name]);
    }
    }
    unset($new_row['customer-names']);
    //move field summary_report_total_qty at end $new_row
    $value = $new_row['summary_report_total_qty'];
    unset($new_row['summary_report_total_qty']);
    $new_row['summary_report_total_qty'] = $value;
    $_SESSION['woe_summary_products'][$key] = $new_row;
    }

    //make new header now
    $row =$_SESSION['woe_summary_products_header'];
    $pos = array_search("Summary Report Total Quantity", $row);
    if($pos!==false) unset($row[$pos]);
    $row = array_merge($row, $woe_summary_products_headers_extra,["Summary Report Total Quantity"]);
    array_unshift($_SESSION['woe_summary_products'],$row);
    });

    Thread Starter samrot

    (@samrot)

    Incredible! Thank you.

    From this reply I still have two more questions…

    2. I updated 2nd code (for CSV) at page https://docs.algolplus.com/algol_order_export/export-products-as-columns/

    You must uncomment line 82!

    1. How can we add a Title “Total” to the $summary_row?
    2. We tried to apply a filter by product taxonomy to this export but it didn’t work. It seems to be filtering the orders that do not contain products with this taxonomy but within those orders it still export products without the taxonomy (even though we have unchecked the options “Export all products from the order”). How can we make this work with this code?
    Plugin Author algol.plus

    (@algolplus)

    2. I updated 2nd code (for CSV) at page https://docs.algolplus.com/algol_order_export/export-products-as-columns/
    Both issues fixed.

    You must uncomment line 99!

    Thread Starter samrot

    (@samrot)

    Great, thx!

    1. Can you do the same for the xls version? (add a Title “Total” to the $summary_row)
    2. The xls version says it has to be exported “w/o Progressbar”. Is this exported automatically this way when exported as an scheduled job or from the profile tab?
    Plugin Author algol.plus

    (@algolplus)

    2.Yes, scheduled jobs run in this mode

    1. please, replace code with this version , I marked change with “NEW”

    //4
    function analyze_products_add_columns($phpExcel,$formatter) {
    // add products as titles
    foreach($this->product_columns as $pos=>$text){
    $formatter->objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow( $pos+$this->headers_added, 1, $text );
    //add formulas for total
    $L = PHPExcel_Cell::stringFromColumnIndex($pos + $this->headers_added);
    $row = $formatter->last_row;
    $formatter->objPHPExcel->getActiveSheet()->setCellValue( $L. ($row+1), "=SUM({$L}2:{$L}$row)");
    }
    $formatter->objPHPExcel->getActiveSheet()->setCellValue( "A". ($row+1), "Total");//NEW
    //make first bold
    $last_column = $formatter->objPHPExcel->getActiveSheet()->getHighestDataColumn();
    $formatter->objPHPExcel->getActiveSheet()->getStyle( "A1:" . $last_column . "1" )->getFont()->setBold( true );
    }
    Thread Starter samrot

    (@samrot)

    Perfect, thx!

    When I try to export client as columns I get the following error:

    Fatal error: Uncaught TypeError: Cannot access offset of type string on string in (…)/plugins/woocommerce-order-export/classes/formats/storage/class-woe-formatter-storage-summary-session.php:47 Stack trace: #0 (…)/plugins/woocommerce-order-export/classes/formats/class-woe-formatter-xls.php(103): WOE_Formatter_Storage_Summary_Session->load() #1 (…)/plugins/woocommerce-order-export/classes/core/class-wc-order-export-engine.php(214): WOE_Formatter_Xls->__construct() #2 (…)/plugins/woocommerce-order-export/classes/core/class-wc-order-export-engine.php(468): WC_Order_Export_Engine::init_formater() #3 (…)/plugins/woocommerce-order-export/classes/admin/tabs/ajax/trait-wc-order-export-admin-tab-abstract-ajax-export.php(25): WC_Order_Export_Engine::build_file() #4 (…)/plugins/woocommerce-order-export/classes/class-wc-order-export-admin.php(540): WC_Order_Export_Admin_Tab_Abstract->ajax_preview() #5 (…)/wp-includes/class-wp-hook.php(324): WC_Order_Export_Admin->ajax_gate() #6 (…)/wp-includes/class-wp-hook.php(348): WP_Hook->apply_filters() #7 (…)/wp-includes/plugin.php(517): WP_Hook->do_action() #8 (…)/wp-admin/admin-ajax.php(192): do_action() #9 {main} thrown in (…)/plugins/woocommerce-order-export/classes/formats/storage/class-woe-formatter-storage-summary-session.php on line 47

    Plugin Author algol.plus

    (@algolplus)

    please, submit your settings as new ticket to https://algolplus.freshdesk.com/

    use tab “Tools” to get these settings.

    Thread Starter samrot

    (@samrot)

    ok, submitted 🙂

    Thread Starter samrot

    (@samrot)

    JIC anyone have this same problem it turned out it was a cookies problem, once deleted cookies from the website, the problem was solved

Viewing 15 replies - 1 through 15 (of 15 total)

The topic ‘Export Clients as columns’ is closed to new replies.