Simple way to Display Random Products in Magento
As a Magento programmer, sometimes we need to show random products in Magento. Here in this post I have provide you with simple Magento code for displaying random products within Magento template.
Below code will get random product using order(‘rand()’) and set product collection.
- <?php
- $collection = Mage::getResourceModel(‘catalog/product_collection’);
- Mage::getModel(‘catalog/layer’)->prepareProductCollection($collection);
- $collection->getSelect()->order(‘rand()’);
- $collection->addStoreFilter();
- $this->setProductCollection($collection);
- return parent::_beforeToHtml();
- ?>
Below code will be written in Magento template file for getting product collection
- if (($_products = $this->getProductCollection())):
- echo $_product->getSku();
- endif;
Hope it will work for you.
Follow @phpzag

Thank you for helping out, great info. “If you would convince a man that he does wrong, do right. Men will believe what they see.” by Henry David Thoreau.
“Quel est mon signe astrologique astrology au”