Viewing 13 replies - 1 through 13 (of 13 total)
  • Plugin Author algol.plus

    (@algolplus)

    Hello

    to use SKU – you should replace
    $product_name = $item['name'];
    with

    $product   = $order->get_product_from_item( $item )
    $product_name = $product->get_sku(); 
    Plugin Author algol.plus

    (@algolplus)

    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);
    
    Thread Starter schmago

    (@schmago)

    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 πŸ™

    Plugin Author algol.plus

    (@algolplus)

    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 2 years, 5 months ago by algol.plus.
    Thread Starter schmago

    (@schmago)

    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-

    Plugin Author algol.plus

    (@algolplus)

    Please, just drag them from section Setup Fields>Products(Product Order Items)

    Thread Starter schmago

    (@schmago)

    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).

    Plugin Author algol.plus

    (@algolplus)

    ok, I see the problem.
    I’ll provide updated version tomorrow .

    Plugin Author algol.plus

    (@algolplus)

    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();
    Thread Starter schmago

    (@schmago)

    As usual, amazing work!
    Huge help for us, thank you!

    Plugin Author algol.plus

    (@algolplus)

    so everything is ok ? any notes ?

    • This reply was modified 2 years, 5 months ago by algol.plus.
    Thread Starter schmago

    (@schmago)

    yes, works well!

    Plugin Author algol.plus

    (@algolplus)

    Ok, got it

Viewing 13 replies - 1 through 13 (of 13 total)
  • The topic ‘SKU by columns and CSV format’ is closed to new replies.