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,

    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
    

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

    "sku","qty"
    "prod1","11"
    
  • 2

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

    <?
    
     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->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);
     }
     
     
     
     $stockItem->save();
    
     unset($stockItem);
     unset($product);
     }
     
     echo "<br />Stock updated $sku";
     
     }
     fclose($file);
     
    ?>
    
  • 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!

  • http://www.facebook.com/countocram Marco Paulo Diala

    Thank you for this! It’s working great on Magento 1.6.1.0. 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

    Thanks.