This question already has an answer here:
- show random products from a category 2 answers
For a product page I'd like to show 1 random product from each of the 3 preselected categories. The final layout would be 3 products next to each other (1 from each category). Now I have read several topics about the random product issue, none of them however address the issue of having several single products from different categories.
I started by creating the code that would pick a random product from a specific category:
<?php $catid = '338'; $category = new Mage_Catalog_Model_Category(); $category->load($catid); $collection = $category->getProductCollection(); Mage::getModel('catalog/layer')->prepareProductCollection($collection); $collection->getSelect()->order('rand()'); $collection->addStoreFilter(); $numProducts = 1; $collection->setPage(1, $numProducts)->load(); ?> <?php foreach($collection as $product): ?> <?php $product = Mage::getModel('catalog/product')->load($product->getId()); echo $product->getName(); echo $product->getShortDescription(); ?> <?php endforeach; ?> <?php endif; ?>
This works just fine. What would be the next step to get 1 product from the other 2 categories (say id 339 & 340)?