I am trying to retrieve a limited number (three) of products from the catalog. Here's my (not working) code:
$products = Mage::getModel('catalog/product')->getCollection();
$products->addAttributeToSelect('*');
$products->getSelect()->order('rand()');
$products->getSelect()->limit(3);
foreach ($products as $product) {
// Do stuff with $product
}
The problem is I am getting sometimes only one product and sometimes no products. Although I have more than three products in the catalog.
Edit:
Here is the query computed by $products->getSelect();
:
SELECT `e`.* FROM `miiny_catalog_product_entity` AS `e` ORDER BY rand() ASC LIMIT 3
I'm using Magento 1.9.2.4.
Thanks in advance.
Louis.