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 ...

  • 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"
  • Copy the code below into a new file, ./quick_updateStock.php

    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 "Stock updated $sku";
    }
    
    fclose($file);
  • 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!