Mass update stock levels in Magento – FAST

This isn’t ground-breaking code, but rather just using some core code in a rather stripped out manner. As we’re not big fans of Magento data flow here, we perform most of our stock and catalogue updates using external scripts to Mage::app. We have been trickling elements of these out to the wider world so you can also benefit from them.

Managing large catalogues and updating stock levels regularly can be mission impossible in Magento, but not any more. With this script – we processed 120,000 stock updates in the blink of an eye. Next time we’ll teach you how to import products at 0.7 seconds each …

  • 1

    Create a CSV with a minimum of 2 columns, the SKU and any of the following,


    Then save it to ./app/var/import/updateStockLevels.csv. For examples sake, we will use,

  • 2

    Copy the code below into a new file, ./quick_updateStock.php

     define('MAGENTO', realpath(dirname(__FILE__)));
     require_once MAGENTO . '/app/Mage.php';
     $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;
     if ($count == 1) continue;
     #Convert the lines to cols 
     if ($count > 0) { 
     foreach($cols as $col=>$value) {
     ${$col} = $line[$value];
     // Check if SKU exists
     $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku); 
     if ( $product ) {
     $productId = $product->getId();
     $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);
     echo "<br />Stock updated $sku";
  • 3

    Visit the file above in your browser, or via command line. It runs very quickly (approx 0.03 seconds per product), so don’t be too surprised if its over quickly!
  • Matt

    Thanks, works great in 1.6.1!

  • Marco Paulo Diala

    Thank you for this! It’s working great on Magento I have to change the directory for the CSV file though because the suggested directory has been moved already. In my case, I just uploaded the CSV file in VAR directory. Thanks again!

  • billybjr

    I’ve been using this script to update stock.  It works great.  I need to change the names of a group of my skus.    I put sku,name and all of the sku’s names to change and ran the script.  It showed successful after it ran but names were not changed… any ideas to make this work?  Thanks

  • Kevin park

    Code is working for
    “sku”,”qty” but if I add any more fields after that like “is_in_stock” code does not import them into DB. It’s only importing field right after “sku”. Is there any way to fix that?

    I am working with latest magento 1.7.1


  • jeff

    We have been using stockupdater from – very easy to use, can even use with a barcode scanner, makes it pretty easy to add and remove inventory as you are checking in or out.

  • Alex

    We are using Advanced Admin Products Manager by Iksanika for any individual and bulk/mass products update activities, including stock managemet. Easy to use and much useful comparing to coding.

  • alan

    and what about magneto extensions? Do anybody use it for stock status?

    I use this one

    Now i can say that everything works perfect.

    maybe somebody can yell some words about others.

    It would be interested listen your opinion and experience

  • Magento Developer

    If you want an easier & faster approach for updating stock via csv then you can following the following ready made script for free:

  • Alex

    I’m using free Stock Inventory Manager extension for mass stock level and statuses update, it is helpful extension which save huge amount of my time. I think only one is better to Stock Invetory Manager it is Advanced Admin Products Manager, from description it is much powerfull solution, unfortunatelly paid one :)

  • webstores

    is_in_stock is not getting updated.
    import file can be found here –

  • Alex

    For people in conversation who would like to proceed products prices attributes update in bulk – you can look to extensions Mass Prices Updater. Hope it will help.

  • Mike Bolter

    This script works great for updating qty, but it doesn’t seem to be updating is_in_stock properly. 1 = yes 0 = no. Correct? or is this just out of date?

  • D. M.

    You don’t have to insert a $product->save() in this script to work???

    The $product->save() is the most slow part on the script.. how this could work without $product->save???


  • Stefanie

    is_in_stock does not work for me either.

  • nav

    could anyone design a script like this for my loaded commerce site? if so let me know