Magento – How to improve product inventory daily updates from CSV file

csvimportinventoryproduct

I'm working on a 10000 products Magento store. I scheduled a cronjob to run everyday at 7am to read a CSV file and update each product inventory and set it out of stock if new QTY is less than 1.

Currently, the update process takes around an hour (more/less) to update all 10000 products. My code as follow:

 $csv = new Varien_File_Csv();
 $csvData = $csv->getData('products.csv');

 foreach($csvData as $k => $v){
     // code to store product_id and new_qty into a temporary table 
 }

 $stock_item_model = Mage::getModel('cataloginventory/stock_item');
 $stock_item_collection = $stock_item_model->getCollection();

 foreach($stock_item_collection as $stock_item) {

     // load new_qty from temporary table based on product_id
     $new_qty = Mage::getResourceModel('custom/temp')->getQty($stock_item->getProductId())

     // set new QTY
     $stock_item->setQty($new_qty);

     // check if in stock or out of stock
     if($new_qty < 1) {
         $stock_item->setIsInStock(0);
     } else {
         $stock_item->setIsInStock(1);
     }   
 }

 // save all
 $stock_item_collection->walk('save'); // this takes around an hour to finish.

So, is there any other way to decrease the update time? Do I need to change the way I'm saving stock items?

Best Answer

try changing indexes to manual, then reindex everything once the import id done (and put the indexes back to auto). if that doesn't help, try working with the database directly - that might depend on your store setup, if you rely on observers when products go out of stock or back in stock.

Related Topic