RSS

Monthly Archives: February 2012

How to get random product of a category in magento

1) To Show the list of all product of a category in random order just copy & paste the following code anywhere in your magento template files:

<?php
$catid = '12';
$category = new Mage_Catalog_Model_Category();
$category->load($catid); // this is your special offers category id!
$collection = $category->getProductCollection();
Mage::getModel('catalog/layer')->prepareProductCollection($collection);
$collection->getSelect()->order('rand()');
$collection->addStoreFilter();
$numProducts = 5;
$collection->setPage(1, $numProducts)->load();
foreach($collection as $product) {
$product = Mage::getModel('catalog/product')->load($product->getId()); /* Load Products by ID*/
echo $product->getName();
echo $product->getShortDescription();
endforeach;
?>
 
3 Comments

Posted by on Feb 27, 2012 in Magento

 

Tags: , , , ,

 
%d bloggers like this: