Magento : Product stock quantity

From here you can get any product’s stock  quantity information like quantity (qty), minimum quantity (min_qty), stock availability (is_in_stock), minimum and maximum sale quantity (min_sale_qty and max_sale_qty).

Load Product

For that you need to load product. There are two ways to load product

1. Load by product id

$id = 12; // put custom product_id
$_product = Mage::getModel('catalog/product')->load($id);

2. Load by Product Sku

$sku = "bloga2bizz"; //custom product sku
 $_product = Mage::getModel('catalog/product')->loadByAttribute('sku', $sku);

 

Get Product stock information

$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product);

You can check stock data in following way:-

echo "<pre>"; print_r($stock->getData()); echo "</pre>";

or

echo $stock->getQty();
echo $stock->getMinQty();
echo $stock->getMinSaleQty();

Top selling product collection programmatically in magento

Top selling products :

Here is way to get best selling product collection.

<?php 
/* Best Seller*/

$visibility = array(
Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
);

$storeId = Mage::app()->getStore()->getId(); // get default store id

$_productCollection = Mage::getResourceModel('reports/product_collection')
     ->addAttributeToSelect('*')
     ->addOrderedQty()
     ->addAttributeToFilter('visibility', $visibility)
     ->setOrder('ordered_qty', 'desc');

 print_r($_productCollection);
?>

Product price update globally in magento using direct sql query

Update product price globally
<?php

$priceToAdd = 10; // custom product price

$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$write->query("
  UPDATE catalog_product_entity_decimal val
  SET  val.value = (val.value + $priceToAdd)
  WHERE  val.attribute_id = (
     SELECT attribute_id FROM eav_attribute eav
     WHERE eav.entity_type_id = 4 
       AND eav.attribute_code = 'price'
    )
");

// where eav.entity_type_id=4 is default attribute id

 

Update all prices from a certain store

This technique is useful when working in a multi-store Magento environment.

<?php

$priceToAdd = 6;
$storeId = 4;

$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$write->query("
  UPDATE catalog_product_entity_decimal val
  SET  val.value = (val.value + $priceToAdd)
  WHERE  val.attribute_id = (
     SELECT attribute_id FROM eav_attribute eav
     WHERE eav.entity_type_id = 4 
       AND eav.attribute_code = 'price'
    )
    AND val.store_id = $storeId
");

 

Update all product prices with a certain attribute set
<?php

$customPriceToAdd = 10;
$attributeSetId = 4;

$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$write->query("
  UPDATE catalog_product_entity_decimal val
  SET  val.value = (val.value + $customPriceToAdd)
  WHERE  val.attribute_id = (
     SELECT attribute_id FROM eav_attribute eav
     WHERE eav.entity_type_id = 4 
       AND eav.attribute_code = 'price'
    )
AND entity_id = ( 
   SELECT p.entity_id FROM catalog_product_entity p
   WHERE p.attribute_set_id = $attributeSetId
)
");

 

Update the Special Price
<?php

$custompriceToAdd = 10;

$write = Mage::getSingleton('core/resource')->getConnection('core_write');
$write->query("
  UPDATE catalog_product_entity_decimal val
  SET  val.value = (val.value + $custompriceToAdd)
  WHERE  val.attribute_id = (
     SELECT attribute_id FROM eav_attribute eav
     WHERE eav.entity_type_id = 4 
       AND eav.attribute_code = 'special_price'
    )
");