• As far as I can understand, your plugin creates an array with the ajax action wp_ajax_wsm_get_products_or_export and puts all the products and variations in this array. The problem is your plugin overwrites the added variations and products because every iterations starts using the offset as the next id, even if the last iteration sets the last id farther.

    To solve it I modified your plugin: the ajax call now requires the item variable, and the json answer returns the last ID added in the array.

    woocommerce-stock-manager.php

    add_action( 'wp_ajax_wsm_get_products_or_export', 'wsm_get_products_or_export' ); 
        function wsm_get_products_or_export(){
        
            $jump = 10;
    
            $i = sanitize_text_field( $_POST['item'] );
            $offset = sanitize_text_field( $_POST['offset'] );
            $posts_per_page = $jump;
            $args = array(
                'post_type'      => 'product',
                'posts_status'   => 'publish',
                'posts_per_page' => $posts_per_page,
                'offset'         => $offset
            );
            $_products = new WP_Query( $args );
            if( !empty( $_products->posts ) ){
                $data = array();
                $i = 1 + $i;
                foreach( $_products->posts as $item ){
    
                    $product = wc_get_product( $item->ID );
                    if( !empty( $product->get_sku() ) ){ $sku = $product->get_sku(); }else{ $sku = ''; }
                    $product_name = $item->post_title;
                    if( !empty( $product->get_manage_stock() ) ){ $manage_stock = $product->get_manage_stock(); }else{ $manage_stock = ''; }
                    if( !empty( $product->get_stock_status() ) ){ $stock_status = $product->get_stock_status(); }else{ $stock_status = ''; }
                    if( !empty( $product->get_backorders() ) ){ $backorders = $product->get_backorders(); }else{ $backorders = ''; }
                    if( !empty( $product->get_stock_quantity() ) ){ $stock = $product->get_stock_quantity(); }else{ $stock = ''; }
                    $product_type = $product->get_type();
    
                    $data[$i]['id']   = $item->ID; 
                    $data[$i]['sku']          = $sku; 
                    $data[$i]['product_name'] = $product_name; 
                    $data[$i]['manage_stock'] = $manage_stock; 
                    $data[$i]['stock_status'] = $stock_status; 
                    $data[$i]['backorders']   = $backorders; 
                    $data[$i]['stock']        = $stock; 
                    $data[$i]['type']         = $product_type; 
                    $data[$i]['parent_id']    = ''; 
                    
                    $i++;
    
                    if($product_type == 'variable'){
    
                        $args = array(
                            'post_parent' => $item->ID,
                            'post_type'   => 'product_variation', 
                            'numberposts' => -1,
                            'post_status' => 'publish' 
                        ); 
                        $variations_array = get_children( $args );
                        foreach($variations_array as $vars){
    
                            $item_product = wc_get_product($vars->ID);
                            if( !empty( $item_product->get_sku() ) ){ $sku = $item_product->get_sku(); }else{ $sku = ''; }
                            
                            $product_name = '';
                            foreach($item_product->variation_data as $k => $v){ 
                                $tag = get_term_by('slug', $v, str_replace('attribute_','',$k));
                                if($tag == false ){
                                    $product_name .= $v.' ';
                                }else{
                                    if(is_array($tag)){
                                        $product_name .= $tag['name'].' ';
                                    }else{
                                        $product_name .= $tag->name.' ';
                                    }
                                }
                            }
    
                            if( !empty( $item_product->get_manage_stock() ) ){ $manage_stock = $item_product->get_manage_stock(); }else{ $manage_stock = ''; }
                            if( !empty( $item_product->get_stock_status() ) ){ $stock_status = $item_product->get_stock_status(); }else{ $stock_status = ''; }
                            if( !empty( $item_product->get_backorders() ) ){ $backorders = $item_product->get_backorders(); }else{ $backorders = ''; }
                            if( !empty( $item_product->get_stock_quantity() ) ){ $stock = $item_product->get_stock_quantity(); }else{ $stock = ''; }
                            $product_type = 'product-variant';
    
                            $data[$i]['id']   = ''; 
                            $data[$i]['sku']          = $sku; 
                            $data[$i]['product_name'] = $product_name; 
                            $data[$i]['manage_stock'] = $manage_stock; 
                            $data[$i]['stock_status'] = $stock_status; 
                            $data[$i]['backorders']   = $backorders; 
                            $data[$i]['stock']        = $stock; 
                            $data[$i]['type']         = $product_type; 
                            $data[$i]['parent_id']    = $item->ID; 
                    
                            $i++;
    
                        }
                    }
    
                }
                $offset = $offset + $jump;
                //$data = json_encode( $data );
                $reponse = array(
                    'status'    => 'continue',
                    'data'      => $data,
                    'offset'    => $offset,
                    'item'      => $i-1
                );
                echo json_encode( $reponse );
                exit();
    
            }else{
    
                $reponse = array(
                    'status'    => 'finish',
                    'text'      => __( 'All done! Close.', 'stock-manager' )
                );
                echo json_encode( $reponse );
                exit();
    
            }
    
        }

    admin/views/import-export.php

    jQuery('.product-export').on( 'click', function(e){  
    
            e.preventDefault();
            jQuery( '.export-output' ).empty();
            jQuery( '#csv' ).append( 'Generating csv file' );
            jQuery( '#csv' ).css( 'display','block' );
    
            var offset = '0';
            var item = '0';
            wsm_export_products( offset, item );        
    
            function wsm_export_products( offset, item ) {
                
                var data = {
                    'action'  : 'wsm_get_products_or_export',
                    'offset'  : offset,
                    'item'    : item
                };
    
                jQuery.post(ajaxurl, data, function( response ) {
                    
                    var result = jQuery.parseJSON( response );
                    
                    if( result.status != 'finish' ){
                        
                        var jsonObject = JSON.stringify(result.data);
    
                        jsonObject = jsonObject.slice( 1 );
                        jsonObject = jsonObject.slice(0, -1);
    
                        jQuery( '.export-output' ).append( jsonObject + ',' );
    
                        wsm_export_products( result.offset, result.item );
                        
    
                    }else{
                        
                        var string = jQuery( '.export-output' ).text();
                        string = string.slice(0, -1);
                        
                        string = '{' + string + '}';
    
                        jQuery( '#csv' ).empty();
                        jQuery( '#csv' ).append( 'All done!' );
    
                        var data = {
                            'action'  : 'wsm_get_csv_file',
                            'data'   : string
                        };
    
                        jQuery.post(ajaxurl, data, function( response ) {
    
                            var data, filename, link;
                            var csv = convertArrayOfObjectsToCSV({
                                datas: JSON.parse( response )
                            });
                            if (csv == null) return;
    
                            filename = 'stock-manager-export.csv';
    
                            if (!csv.match(/^data:text\/csv/i)) {
                                csv = 'data:text/csv;charset=utf-8,' + csv;
                            }
                            data = encodeURI(csv);
    
                            link = document.createElement('a');
                            link.setAttribute('href', data);
                            link.setAttribute('download', filename);
                            link.click();
    
                        });
                
                    }
        
    
                });
            }
        });

    Hope this will help.

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Solution for missing variations during export’ is closed to new replies.