Magento – How to i quick update of qty and stock and all inventory fields in Magento

catalog-optimizeinventoryproductsave

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

     $product=Mage::getModel('catalog/product')->load($id);
     $product->getName();
     $product->setSpecialFromDate('2010-10-28');
    $product->setSpecialFromDateIsFormated(true);
    $product->setSpecialToDate();
    $product->setSpecialToDateIsFormated(true);
   /* 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
$product->load($product_id);

$stockData['qty'] = $v;
if($v>0){
$stockData['is_in_stock'] = 1;
}else{
$stockData['is_in_stock'] = 0;
}

$product->setStockData($stockData);
$product->save();

I want to update inventory quickly.Can anyone help me

Best Answer

<?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->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 "<br />Stock updated $sku";

 }
 fclose($file);

?>

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) {
  $process->setMode(Mage_Index_Model_Process::MODE_MANUAL)->save();
  //$process->setMode(Mage_Index_Model_Process::MODE_REAL_TIME)->save();
}
Related Topic