Simple way to Display Random Products in Magento

July 4th, 2012 by Laeeq | 2 comments

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.

  1. <?php
  2. $collection = Mage::getResourceModel(‘catalog/product_collection’);
  3. Mage::getModel(‘catalog/layer’)->prepareProductCollection($collection);
  4. $collection->getSelect()->order(‘rand()’);
  5. $collection->addStoreFilter();
  6. $this->setProductCollection($collection);
  7. return parent::_beforeToHtml();
  8. ?>

Below code will be written in Magento template file for getting product collection

  1. if (($_products = $this->getProductCollection())):
  2. echo $_product->getSku();
  3. endif;

Hope it will work for you.

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

 

 

  1. coltonsanchezom
    September 28th, 2012 at 08:22 | #1

    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.

  2. December 11th, 2012 at 10:56 | #2

    “Quel est mon signe astrologique astrology au”

  1. No trackbacks yet.