0

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.

3
  • 1
    This code is correct, it should always return 3 random items from the collection.
    – versedi
    Jul 6, 2016 at 9:48
  • I thought it was. Anyway this does not render the result I was hoping for. Thanks! Jul 6, 2016 at 12:32
  • I suggest to search for an error somewhere else. Is there anything appearing in the logs/apache logs when less than 3 products are returned?
    – versedi
    Jul 6, 2016 at 12:34

1 Answer 1

1

try this code

$productsCollection = Mage::getModel('catalog/product')->getCollection();
$productsCollection
         ->addAttributeToSelect('*');
$productsCollection
         ->getSelect()
         ->order('rand()');
$products = $productsCollection
         ->setPageSize(3)
         ->setCurPage(1)
         ->load();

what you recive error? try look log

tailf -100 var/log/system.log

tailf -100 var/log/exception.log

in magento for get more information, about error.

and you can enable error for display on develop server by local.xml in directory errors.

cp errors/local.xml.simple errors/local.xml

3
  • Thank you for the answer. I'm getting the same result (only one product or no products). tail -f -100 var/log/system.log returns 2016-07-06T12:50:02+00:00 ERR (3): Warning: addcslashes() expects parameter 1 to be string, object given in /Users/louisfischer/Projects/miiny/magento/lib/Zend/Db/Adapter/Pdo/Abstract.php on line 296. Also I get every product when removing $products->getSelect()->limit(3); or $products = $productsCollection->setPageSize(3)->setCurPage(1)->load(); Jul 6, 2016 at 12:52
  • you can look you query by echo $productsCollection->getSelect() and please post you query in qwession
    – Naumov
    Jul 6, 2016 at 12:58
  • SELECT `e`.*, `price_index`.`price`, `price_index`.`tax_class_id`, `price_index`.`final_price`, IF(price_index.tier_price IS NOT NULL, LEAST(price_index.min_price, price_index.tier_price), price_index.min_price) AS `minimal_price`, `price_index`.`min_price`, `price_index`.`max_price`, `price_index`.`tier_price` FROM `miiny_catalog_product_entity` AS `e` INNER JOIN `miiny_catalog_product_index_price` AS `price_index` ON price_index.entity_id = e.entity_id AND price_index.website_id = '1' AND price_index.customer_group_id = 0 ORDER BY rand() ASC LIMIT 3. Jul 6, 2016 at 13:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.