Showing posts with label stock. Show all posts
Showing posts with label stock. Show all posts

Monday, October 17, 2011

How to get products available stock quantity in magento?

Here is the code to get produts stock quantity. Place this code in your view.phtml file.
echo $qtyStock = (int) Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();

Wednesday, October 5, 2011

Mass Products Stock levels Update in Magento

Step 1:
Create a CSV with a minimum of 3 columns, the SKU, qty and is_in_stock.
Then save it to "/app/var/import/updateStockLevels.csv".
For instance, we will use,
"sku","qty","is_in_stock"
"prod1","100","1"

Step 2:
Copy the code below into a new file "updateStock.php" and place this file in your root folder
define('MAGENTO', realpath(dirname(__FILE__)));
 require_once MAGENTO . '/app/Mage.php';
 umask(0); $count = 0;
 Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
 
 $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 "
Stock updated $sku"; 
}
 fclose($file);

Step 3:
Run the php script in  Your browser like "www.YourDomain.com/updateStock.php".
Thats it. You are done.