Magento get Product Collection


Magento get Product Collection: Magento product collection is loaded using the model Mage::getModel(‘catalog/product’). You can use this model to get production collection based on your requirement. You can add your filters to the collection if you want data to be filtered based on some criteria.


Syntax for Magento get Product Collection

Here is the syntax to load the magento products in descending order they are created.


$collection = Mage::getModel('catalog/product')
                        ->getCollection()
                        ->addAttributeToSort('created_at', 'DESC')
                        ->load();
foreach ($collection as $product){
   echo $product->getId().'</br>';
   echo $product->getName().'</br>';
}

The above collection will return the magento products.

Magento get Product Collection : Only 10 Products

If You Want to load magento’s top last 10 created products you can load the model as below.


$collection = Mage::getModel('catalog/product')
                        ->getCollection()
                        ->addAttributeToSort('created_at', 'DESC')
                        >setPageSize(10);
                        ->load();
foreach ($collection as $product){
   echo $product->getId().'</br>';
   echo $product->getName().'</br>';
}


Advertisements

Add Comment

📖 Read More