How to fast stock update Magento

If you ever tried to import product stocks in Magento I'm sure you noticed is extremly slow.

Doesn't matter how many fields you set in the CSV file it always takes the same time.

Digging around I found this amazing code.

Create a CSV File


Place your CSV file like I wrote above.

You can add many fields but I would suggest 2 or 3 (to be faster)

qty min_qty use_config_min_qty is_qty_decimal backorders use_config_backorders min_sale_qty use_config_min_sale_qty max_sale_qty use_config_max_sale_qty is_in_stock use_config_notify_stock_qty manage_stock use_config_manage_stock stock_status_changed_automatically type_id

I will use the SKU and the QTY. My CSV file content looks like:

"sku","qty" "123456789","10"
Code language: JavaScript (javascript)


Create a PHP file in your root folder with this content.

<? define('MAGENTO', realpath(dirname(__FILE__))); require_once MAGENTO . '/app/Mage.php'; umask(0); Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); $count = 0; $file = fopen(MAGENTO . '/var/import/updateStockLevels.csv', 'r'); while (($line = fgetcsv($file)) !== FALSE) { if ($count == 0) { foreach ($line as $key=>$value) { $cols[$value] = $key; } } $count++; if ($count == 1) continue; #Convert the lines to cols if ($count > 0) { foreach($cols as $col=>$value) { unset(${$col}); ${$col} = $line[$value]; } } // Check if SKU exists $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku); if ( $product ) { $productId = $product->getIdBySku($sku); $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId); $stockItemId = $stockItem->getId(); $stock = array(); if (!$stockItemId) { $stockItem->setData('product_id', $product->getId()); $stockItem->setData('stock_id', 1); } else { $stock = $stockItem->getData(); } foreach($cols as $col=>$value) { $stock[$col] = $line[$value]; } foreach($stock as $field => $value) { $stockItem->setData($field, $value?$value:0); } $stockItem->save(); unset($stockItem); unset($product); } echo "<br />Stock updated $sku"; } fclose($file); ?>
Code language: HTML, XML (xml)

Run it and have fun!

You're done. Please try it your self and fly like a thunder 😉

Product Import

No there is no fast product import, yet.

  • Hi, i`m using this to update my status and it`s working very well. We have more than 5000 products on database. Sometimes the php function does not ends and timeout. What can i do? Maybe updating half of the stock in each function? Hope you can help me, thank you.

    • Yes, perhaps you could try to do it in two times.

      Upload 2 CSV files with 2500 each.

      If that’s a lot of trouble you may ask for a server upgrade, having more RAM and CPU it might help your store 😉


  • Hey dude.. you rock, but tell me one thing The stock id will automatically will get set? specially visibilty and Is in stock attribute?

  • Hi again, after rechecking your script, No wonder Your php script completely rock but whenever I update stuff my stock availabilty remains out of stock and same with visibilty, Can you tell me how to add these two attributes also?


  • Thankx, Its perfect solution 🙂
    Do you have any idea where can i exceed execution time? I have tried my php.ini and increase execution time but doesnt work, so any other idea?
    It shows me this error: Maximum execution time of 30 seconds exceeded in /home/pentljac/domains/ on line 560

  • Hey, I am looking for some script like this which will add new products in my store but without replacing any information about current products.

    Can you do that?
    It will b great for me.

  • Awesome code – I’ve been looking for something like this for weeks. Thanks!

    By the way, in Magento 1.7 (and maybe other versions, I don’t know) there is no /var/import/ folder, instead they combined the import and export folders into /var/importexport/

  • Thanks for the script. It works beautifully with test CSV file.
    However, my POS creates CSV file but it puts different heading so script does not work. I want to automate the process so is there any way to change the names of headers?

    Script requires the headers to be

    my CSV is

    Is there any way for these to different names to be linked within the script so that script sees ITEM as sku and STOCK as qty?

    Thanks for your help.


    • Hey Kevin,

      Have you actually tried to run the script with your custom fields?

      The script looks at the first line (headers) and uses that key. That why you can set any field in there.

      Let me know,


  • Hello Rick

    Awesome script thanks! I have same problem that csv is using these headers “ITEM","STOCK"

    I tried to run the script with “ITEM","STOCK" in csv but nothing is updated.

    Any suggestions?

    • That’s weird. I’m guessing you’re using Magento 1.7, right?

      Have you tried using other fields?

      Do you have the magento cache on?

      Did you rewrite the index catalog?


  • Hey Rick, You made a simple, but great solution for our problem. Magento 1.7 import has a BUG : error in qty, and your script saved me for $2000.


  • Thanks, it s working Great !!

  • Hi Rick,

    You are a superstar!!! I love this script thanks a million it is saving me loads of time! I was wondering if there is anyway to alter the script so that teh headers are pre-defined and do not rely on the first row. The reason I ask is that my supplier provides me with a csv that has the ‘sku” in the first column and the “qty” in the second, with no row labels at the top?

    Obviously your are busy, and only if it is a easy change I know you are doing this for free so no need to worry if you have not got the time.



    • Hi Danny,

      I guess you could hardcode the values without the first row.

      However you should try to ask your supplier, maybe he can change his output so you get the perfect CSV.


  • FYI instead of unset() you could call $obj->clearInstance(); – better way to free memory in a loop when you’re using Magento models.

  • Hi Rick!

    This is really great job!
    Can you just add one more thing so that I can add qty from current qty such as, “123456”, “+10” or “123456”,”-5″
    Thank you again.

    • Wrote this post 4 years ago, does this still work on Magento 1.9.1? 🙂

      I would guess you need to play around this line:

      $stockItem->setData($field, $value?$value:0);
      Code language: PHP (php)

      Don’t have the time to do testing though.

      If you happen to find the solution, please let us know!

  • Hello Rick,
    Great code and it works great. Thank you for the ideas, I had a huge problem when there is a need for updates over 1000 items. It takes a bit of time to crunch all data. I have a client who has updates every 30 minutes and it varies from 100 to 4000 items avg. Plus once a day a 5000 items once a day. The server will hang or cut off the process because it takes too long to run, which can be from 2 to 20 minutes to run.
    The solution we came up with was to write a pure Mysql script. We can update 1000 items in about 1 second. Yes 1 second per 1000 items. Now the server and client are very happy. Mysql is the way to go if the problem is volume.

    Thanks again,

  • We changed the paths but yes still works.

  • If my csv is
    “sku”,”qty”, “is_in_stock”
    “AA”,”0″, “0”
    “BB”, “100”, “1”

    This updates the database just fine, my issue is getting the page to update.

    I run Mage::app()->getCacheInstance()->flush(); but the out-of-stock still don’t change. The only way it works for me is if I go to System > Cache Management > Flush Full Page Cache then my page changes.

    Just wondering if there is way via code.
    Using Magento ver.

    • It’s been too long since I’ve last played with Magento. Hopefully another user might be able to help you.

      Should you find the solution, please let us know! 😉

  • Can you tell me how to import CSV

  • Hey,
    thanks for this script. Is there a way to update the price too?

Leave a Reply

Add <code> Some Code </code> if you need to.