Question

I'm trying to import stock using csv. When I click on submit, the import functionality works fine. But when i click on reload when it is posting then it loads until the post function completes.

<?php
if(isset($_POST['submit'])){
    if (!file_exists($mediaPath.'csv')) {
        mkdir($mediaPath.'csv', 0777, true);
    }
    $file_type = $_FILES['file']['type']; //returns the mimetype
    $allowed = array('text/csv');
    if(!in_array($file_type, $allowed)) {?>
    <h1 class="error">Only CSV files allowed</h1>
    <tr>
        <td>Wrong format</td>
        <td>Wrong format</td>
        <td>Wrong format</td>
    </tr>
    <?php
   $model->addData([
    "title" => 'Title 01',
    "content" => 'Content 01',
    "summary" => "true",
    "create_at" => new \Zend_Db_Expr('NOW()')
    ]);
    $saveData = $model->save();

    }else{    
    $displayFlag = 1;
    $csv =  $_FILES['file'];    
    $targetdir = $mediaPath;   
    $image_name=$_FILES['file']['name'];
    $temp = explode(".", $image_name);
    $newfilename = round(microtime(true)) . '.' . end($temp);
    $imagepath=$mediaPath."csv/".$image_name;
    if(move_uploaded_file($_FILES["file"]["tmp_name"],$imagepath)){
        $csvFile = file($imagepath);
        $data = [];
        foreach ($csvFile as $line) {
            $data[] = str_getcsv($line, ",", '"');
        }
        $keys = [];
        $result = [];
        foreach($data as $key => $value){
            if($key == 0){
                $keys = $value;
            }            
        }
        foreach($data as $key => $value){
            if($key !== 0){
                $result[] = array_combine($keys, $value);
            }            
        }
        foreach($result as $key => $value){
            if(isset($value['sku'])){
                if($product->getIdBySku($value['sku'])) {
                    $stockItem = $stockRegistry->getStockItemBySku($value['sku']);
                    $stockItem->setQty($value['qty']);
                    $sku = $value['sku'];            
                    if($stockRegistry->updateStockItemBySku($sku, $stockItem)){                
                        ?>
                        <tr>
                            <td><?php echo $value['sku'] ?></td>
                            <td><?php echo $value['qty'] ?></td>
                            <td>Updated</td>
                        </tr>
                    <?php
                    }else{?>
                        <tr>
                            <td><?php echo $value['sku'] ?></td>
                            <td><?php echo $value['qty'] ?></td>
                            <td>Updated</td>
                        </tr>
                        <?php
                    }
                }else{ ?>
                     <tr>
                            <td><?php echo $value['sku'] ?></td>
                            <td><?php echo $value['qty'] ?></td>
                            <td>Sku Not found</td>
                        </tr>
                <?php
                }
            }else{
                ?>
                 <h1 class="error">Only CSV files allowed</h1>
                    <tr>
                        <td>Wrong format</td>
                        <td>Wrong format</td>
                        <td>Wrong format</td>
                    </tr>
            <?php 
                break;
            }            
        }
    }
}
header('Location: http://192.168.31.236/magento/admin/grid/grid/index');
exit();
}
?>
</table>

I can't load any admin pages until this post function completed.

No correct solution

OTHER TIPS

Check this

https://github.com/DominicWatts/StockUpload

You are recreating so much functionality I think it would help to look at something already built

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top