update stock quantity in Magento

In this article you learn how to update stock quantity of a product by coding...
If you want to update quantity which product have more then 10 quantity change in 50 quantity.
So if you do by admin then will take lots of time. Here by coding is easy way,...
code run in root dir.

Here is full code....

<?php
 $mageFilename = 'app/Mage.php';
 require_once $mageFilename;
 Mage::setIsDeveloperMode(true);
 ini_set('display_errors', 1);
 umask(0);
 Mage::app('admin');
 set_time_limit(0);
 ini_set('memory_limit','1024M');

 $newstock = 100;

 $productCollection = Mage::getModel('catalog/product')
   ->getCollection()
   ->addAttributeToSelect('*')
   ->joinField('qty',
      'cataloginventory/stock_item',
      'qty',
      'product_id=entity_id',
      '{{table}}.is_in_stock=1',
      'left')
   ->addAttributeToFilter('qty', array("gt" => 30));

 foreach($productCollection as $product)
 {
  $producte = Mage::getModel('catalog/product')->load($product->getId());
  $stockQty = $producte->getStockData();
  $stockQty['qty'] = $newstock;
  $stockQty['is_in_stock'] = 1;
  $producte->setStockData($stockQty);
  $producte->save();
 }

?>

Labels: