Updating products in bulk is manually is only feasible if there are few countable products on your fingers. But as the product catalog increases updating products values becomes a hectic task. And there comes solution of updating in bulk programmatically.

The logic is accessing product objects and updating particular attribute.

In this article i’m writing how to update product inventory stock, backorders, quantity and stock availability. On the below code snipped i have specifically updated ids listed on a cvs file for a particular project requirement. You can ignore those and update in bulk or place conditions as according to your need. At the end it’s about updating attribute value.

Save the below code snippet as `filename.php` file and place in magento project root and run.


$inventoryupdatedarr = array();
$file = fopen('inventoryupdated.csv', 'r'); //the csv file contains product ids on 1st column 
while (($line = fgetcsv($file)) !== FALSE) {
  array_push($inventoryupdatedarr, $line[0]);

$collection = Mage::getModel('catalog/product')
                        ->addAttributeToFilter('type_id', array('eq' => 'simple'))
                        ->addAttributeToFilter('obsolete',0); //all live products this is just in my case, ignore this.

//Load your product object
foreach ($collection as $product) {
	if(!in_array($product->getId(), $inventoryupdatedarr)){ //only updating ids listed on csv file. only in my case, you can ignore this.
		$product_id = $product->getId();
		$qty = 0;
		$_product = Mage::getModel('catalog/product')->load($product_id);
		// If product exists, get the inventory items information
		if ($_product) {
		      $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);
		      // If there's no inventory information, create a registry
		        if (!$stockItem->getId()) {
		            $stockItem->setData('product_id', $product_id);
		            $stockItem->setData('stock_id', 1);
		            $stockItem->setData('manage_stock', 1);
		            $stockItem->setData('use_config_backorders', '1');
		            // $stockItem->setData('qty', $qty);
		        } else { // if there is, update it
		            $stockItem->setData('use_config_backorders', '1');


Suman K.C

eCommerce Developer, writes about stuff related to eCommerce development. Currently based in Kathmandu, loves reading about startups & new technology, introvert nature, fond of traveling & playing futsal.

More Posts