If you want to show products from a specific category on your home page you can do this simply with
{{block type="catalog/product_list" category_id="12" template="catalog/product/list.phtml"}}
on your home page which works fine.. however, if you want these products to be randomly selected you hit problems.
The obvious thing to try is
{{block type="catalog/product_list_random" category_id="12" template="catalog/product/list.phtml"}}
however, this displays random products from EVERY category!!
The reason for this is that the file random.php does not work as advertised so we need to create a new version that does. We do not want to break upgrade compatibility so create the following directory structure.
in app/code/local create Mage/Catalog/Block/Product/List
eg mkdir -p Mage/Catalog/Block/Product/List
In your new List directory create the following file called Random.php
<?php class Mage_Catalog_Block_Product_List_Random extends Mage_Catalog_Block_Product_List { protected function _getProductCollection() { if (is_null($this->_productCollection)) { $categoryID = $this->getCategoryId(); if($categoryID) { $category = new Mage_Catalog_Model_Category(); $category->load($categoryID); // this is category id $collection = $category->getProductCollection(); } else { $collection = Mage::getResourceModel('catalog/product_collection'); } Mage::getModel('catalog/layer')->prepareProductCollection($collection); $collection->getSelect()->order('rand()'); $collection->addStoreFilter(); $numProducts = $this->getNumProducts() ? $this->getNumProducts() : 3; $collection->setPage(1, $numProducts)->load(); $this->_productCollection = $collection; } return $this->_productCollection; } }
To call this on your home page open your Home page in CMS > Static Pages
and in Content add
{{block type="catalog/product_list_random" category_id="YOUR_CATEGORY_ID" template="catalog/product/list.phtml" column_count="4" num_products="12"}}
Create a new hidden category and add the products you wish to randomly select from. Find the category ID of this category and enter this number in the above place marker.
You will find that although you are seeing the Category display tool bar (drid view / list view / show.. etc) it has no effect on the layout. the default layout is 3 x 3 grid which is where column_count=”4″ comes into play. Alter this to meet your themes needs. Same goes for num_products=”12″.
And that is that.
Don’t want to be looking at the grid.. hide it. (evil hack alert)
Add
<style type="text/css"> .toolbar {display:none;} </style>
at the top of your Content area on the homepage CMS. this will hide the tool bar for just the homepage.
References:
Thanks to mac75a here : http://www.magentocommerce.com/boards/viewthread/72319/ and andytm here: http://www.magentocommerce.com/boards/viewthread/72319/