0

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.

0

Check this

https://github.com/DominicWatts/StockUpload

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

3
  • This module is great. But when I import I get this error 1 exception(s): Exception #0 (Magento\Framework\Exception\LocalizedException): The XML in file "/var/www/html/magento/app/code/Xigen/CsvUpload/view/adminhtml/ui_component/xigen_csvupload_csv_listing.xml" is invalid: Element 'argument': This element is not expected. Expected is ( dataProvider ). Line: 16 – Ramesh KR Jun 1 '20 at 4:24
  • @RameshKR I thought maybe issue with extension but I've checked and it is is fine. I've tried developer mode. I've scanned XML. Do you have latest version 1.0.8? I've tested on 2.3.2 store. Perhaps you are using older/newer? – Dominic Xigen Jun 1 '20 at 22:52
  • I'm using the latest 2.3.4 – Ramesh KR Jun 2 '20 at 5:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.