I want quick update of product on MAGENTO C.E 1.7

I know about the quick update of product attribute value using

below code: getResource()->saveAttribute()

just like

   /* quick save */
    $product->getResource()->saveAttribute($product, 'special_from_date');

    $product->getResource()->saveAttribute($product, 'special_to_date');

But i want know how can update product inventory quickly just like above product attribute update

and i have optimized my below code

// call product model and create product object
$product    = Mage::getModel('catalog/product');
// Load product using product id

$stockData['qty'] = $v;
$stockData['is_in_stock'] = 1;
$stockData['is_in_stock'] = 0;


I want to update inventory quickly.Can anyone help me


 define('MAGENTO', realpath(dirname(__FILE__)));
 require_once MAGENTO . '/app/Mage.php';

 $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;


 if ($count == 1) continue;

 #Convert the lines to cols 
 if ($count > 0) { 
 foreach($cols as $col=>$value) {
 ${$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);



 echo "<br />Stock updated $sku";



Taken from Sonassi:

If you need to squeeze more out there are also other options to consider:

Ideally you'll want to disable auto update indexing during imports and re-enable afterwards as this will kick off other queries during the import.

$pCollection = Mage::getSingleton('index/indexer')->getProcessesCollection(); 
foreach ($pCollection as $process) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.