Sam
Sam

Reputation: 6344

Magento: Random "Featured Product"

My client needs a a small box on CMS pages and on Category landing pages that will show thumbnail/price/short description of a random item related to that category (separate from the grid view) Any thoughts on what would be the best way to accomplish this? Thanks, -Sam

Upvotes: 0

Views: 1518

Answers (1)

sulabh
sulabh

Reputation: 1117

go to template/catalog/product/view/ and make a new phtml file random_product.phtml with the following code

<?php
$catId = $this->getCat_id();
$cat=Mage::getModel("catalog/category")->load($catId);
$prodCollection = $cat->getProductCollection();
$pids=array();
foreach($prodCollection as $product)
{
        array_push($pids,$product->getId());
}
$randProductId=array_rand($pids);
echo $randProductId;
?>

now if your category id is for example 10, make a static block and paste the following code in the contents

{{block type="catalog/product" cat_id="10" template="catalog/product/view/random_product.phtml"}}

now when you will view the static block, you will see a random product id every time you refresh. THen,you can write your own custom html in the phtml file after loading the product.

To load your product from here you can do $product = Mage::getModel('catalog/product')->load($randProductId); then call methods such as $product->getName() etc to get the details you need to output.

Upvotes: 2

Related Questions