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->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); ?>
-
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!
Last 5 posts in Magento
Easily integrate Wordpress feeds in Magento
Dependant layered navigation filters in Magento
Custom attributes for categories in Magento
How to disable WYSIWYG on Magento 1.4
Magento 1.4 Install Errors


Tj
8 Feb. 2010
This is great! I have a multiple website install with different B2B setups as well as retail site, all of which have different pricing structures.
Perhaps you guys could do a post for fast updating of prices by store?
ben@sonassi.com
9 Feb. 2010
We certainly could do (in fact, have done), although for pricing – it does take a little longer for product saves, around 0.8s per product.
sean
12 Feb. 2010
Great information, thanks. Looking forward to your post on importing products quickly. I actually need to figure out that aspect of my site right away, but hopefully it will pop up while I work on some other things!
Ted
23 Feb. 2010
Great Post guys. Thank You.
Any idea when we can expect the mass products import script?
ben@sonassi.com
23 Feb. 2010
At some point in the near future
Anh Nguyen
25 Feb. 2010
OMG, this is such a beautiful work. Magento’s importing sucks. I have to spend hours for importing 1000 product stocks. Thank you so much!