How to create a Random Featured Product list on home page in Magento
It seems some what amazing that such a simple request can be so tricky to track down. The scenario is simple. You want to display a set of products on your home page from a hidden category to be randomly displayed.
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/
Rock solid speed and reliability
Robust, reliable, fully backed up, secure UK based Magento hosting for £175 per year. Full details here or Contact us
Share this:
Related posts:
This entry was posted by admin on October 3, 2010 at 11:17 pm, and is filed under Magento. Follow any responses to this post through RSS 2.0.You can leave a response or trackback from your own site.
-
@Vinko: the product’s visibility Flag is done by the method Mage::getModel(‘catalog/layer’)->prepareProductCollection($collection)
Thanks for the help of the whole community.
If I were you I will overwrite too the method _beforeToHtml() with the following lines, no more problem with the toolbar.
class Mage_Catalog_Block_Product_List_Random extends Mage_Catalog_Block_Product_List{
protected function _beforeToHtml()
{
return $this;
}
…
} -
Great solution. So far it’s working, I the 3 products showing up randomly each time I refresh the homepage.
In order to have 3 products instead of 4, I changed (line 35):
Now I need to figure out why there aren’t any images for the products because right now, no images are showing up. Any working solution for that problem?
Also I wanted to know, what is $_collectionSize for? (line 7)
Thanks!
ps- Great blog btw, very useful.
-
#25 written by Peter 1 year ago
Hi there,
Tried this and failed but dying to get this working!
I’m running on 1.4.1.1, I created the random.php (including php closing tag), uploaded to the new directory path in local, included the line in cms (see below) calling the correct block and correct template (custom list without toolbar) with the correct cat ids.
{{block type="catalog/product_list_random" category_id="465" template="catalog/product/list_home.phtml" column_count="4" num_products="4"}}Problem is that it’s still calling from the whole catalogue and not the specified cat ids.
I have 2 of these blocks on the home page using the same template in case that could create the problem? I did try and remove on of them but still the same result.
Would be so incredibly greatful for help on this
All the best
Peter
-
Tomas:
I have same problem as Peter, it callling all products from catalog, not from that category which is specified by id.
I am using 1.6 version
And Jerome, I have seen you comment in more blog’s but anythere you won’t clarifying how to do that.And one more question what do you mean by telling that “create hidden category”, it can be active or not?
I can see that comments here is quite old, but anywhere on web aren’t any good solutions, maybe if someone from blog owners would find a solution to this magento problem whole blog become much more popular? I am ready to make a donation for that
And if not here is a link for extensions which I think would help to achieve needed goals: http://www.magentocommerce.com/magento-connect/product-randomizer-9307.html
-
Thanks for this great code snippet and tutorial!
However, I have one problem using 1.5.1.0 implementing this in a custom module…
If I use this code, I can’t get Magento to generate the AddToCart URL for the products of this collection. It does work throughout the store for all other list views though, so it’s not a store issue I guess…Using this code:
$collection = Mage::getResourceModel('catalog/product_collection')
->addAttributeToFilter('entity_id', array('in' => $productIds))
->addAttributeToSelect('*')
->addFinalPrice();
Mage::getModel('catalog/layer')->prepareProductCollection($collection);
$collection->getSelect()->order( "FIELD (`e`.`entity_id`,'".implode("','",$productIds)."')" );
$collection->addStoreFilter();
$this->products = $collection;return $this->products;
-
Thank you for the code man!
I have just modified the same code and used in a .phtml file, named list_home.phtml
We need to write code for home cms like:
{{block type=”catalog/product_list” category_id=”50″ template=”catalog/product/list_home.phtml”}}Code:
getLoadedProductCollection();
$categoryID = $this->category_id;
//$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() : 1;
$collection->setPage(1, $numProducts)->load();
$_productCollection = $this->_productCollection = $collection;
?>
count()): ?>__('There are no products matching the selection. Please provide a category ID.') ?>
count() ?>
<a href="getProductUrl() ?>" title="htmlEscape($_product->getName()) ?>">
<img src="helper('catalog/image')->init($_product, 'small_image')->resize(105, 105); ?>" width="105" height="105" alt="htmlEscape($_product->getName()) ?>" /> -
A good methode.But here is a more flexible methode to add featured product randomly one by one.
Try this one:
http://www.awebsolution.info/magento-add-random-featured-products-home-pageThnak you
-
Great piece of code, thanks for this !!!
One remark though, it seems to ignore the categori_id=”". Even when blank, or a non-existing ID, it always shows all products from all categories… Just like many comments above already mentioned.
I’m runnin 1.7 RC2, is there any fox for this? even if it’s a dirty-hack directly in Random.php ?
KR
Kamaradski -
- Notes on Magento benchmarking..
- Magetno fix : Cant add products to admin create order
- Magento fix : Invalid method Mage_Wishlist_Model_Item::canConfigure(Array
- Create a multi-store setup in Magento
- Magento fix: Fatal error: Method Varien_Object::__tostring() cannot take arguments in /magento/lib/Varien/Object.php
- Magento fix : Local file doesn’t exist
- Magento tip: after upgrade prices on product page simple product are gone……
- Add attribute to Grid or List view
- Magento tip : add Google Checkout button to side bar shopping cart
- Magento Tip: Add simple product url to grouped product items

This is a great addition to the Home page, but if the feature also take into account the product’s Visibility flag and whether the product is Enable that will be even better.
Don’t you think?