Friday, October 12, 2012

Get Product collection in Magento


 There are many way to get product collection.
 >> First preferable way to get Collection of Magento products :

   $visibility = array(
       Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
       Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG,
       Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_SEARCH
    );
   $productCollection = Mage::getModel('catalog/product')->getCollection()
                                        ->addAttributeToSelect('id')
                                        ->addAttributeToSelect('name')
                                        ->addAttributeToFilter('visibility' , $visibility);

Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($productCollection);
Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($productCollection);

$productCollection->getSelect()->limit(12);