How to Random Products Home Magento

Most of the Magento themes comes with a cool slider or featured products in the home page.

The bad thing is...would not be dinamic.

Today i'll show you How to create a Random Products in your Magento Home page.

Create the Home Products file

In my theme I created a copy of the list.phtml called home_list.phtml

/app/design/frontend/base/theme156/template/catalog/product

Show the file in the Home Page

Now we need to show our new file in the Home Page.

Go to your Admin panel -> CMS -> Pages

You should have a page called something like Home. Open it and paste the following code:

{{block type="catalog/product_list" category_id="36" template="catalog/product/home_list.phtml"}}

NOTE: please notice the category_id. It will force to show a certain category. In my case a special one called Home Products.

Shuffle it

The code is quite simple. We need to add the shuffle function after getting all the products.

Look for the getItems() function and past it below.

<?php $_items = $_productCollection->getItems(); shuffle($_items); ?>

Break on 3

I added a counter to break the foreach. Something like:

// Show 3 items $max_items = 3; $personal_count = 1;

And then at the end:

if($personal_count == $max_items){break;} else{$personal_count++;}

Full Code

Here's my full code of the home_list.phtml. It will depend on your theme but the suffle part should be the same.

<?php /** * Magento * * NOTICE OF LICENSE * * This source file is subject to the Academic Free License (AFL 3.0) * that is bundled with this package in the file LICENSE_AFL.txt. * It is also available through the world-wide-web at this URL: * http://opensource.org/licenses/afl-3.0.php * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to license@magentocommerce.com so we can send you a copy immediately. * * DISCLAIMER * * Do not edit or add to this file if you wish to upgrade Magento to newer * versions in the future. If you wish to customize Magento for your * needs please refer to http://www.magentocommerce.com for more information. * * @category design_blank * @package Mage * @copyright Copyright (c) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com) * @license http://opensource.org/licenses/afl-3.0.php Academic Free License (AFL 3.0) */ ?> <?php /** * Product list template * * @see Mage_Catalog_Block_Product_List */ ?> <?php $userIsloggedIn = Mage::getSingleton('customer/session')->isLoggedIn(); ?> <?php $_productCollection=$this->getLoadedProductCollection() ?> <?php if(!$_productCollection->count()): ?> <p class="note-msg"><?php echo $this->__('There are no products matching the selection.') ?></p> <?php else: ?> <div style="clear:both;">Β </div> <div class="category-products" style="border:none !important;"> <div class="page-indent"> <?php // List mode ?> <?php if($this->getMode()!='grid'): ?> <?php $_iterator = 0; ?> <ol class="products-list" id="products-list"> <?php $list_item=1; foreach ($_productCollection as $_product): ?> <li class="item<?php if($list_item==1){echo ' first ';} ?><?php if( ++$_iterator == sizeof($_productCollection) ): ?> last<?php endif; ?>"> <?php // Product Image ?> <a class="product-image" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>"> <img src="<?php echo $this-/>helper('catalog/image')->init($_product, 'small_image')->resize(205, 181); ?>" width="205" height="181" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" /></a> <?php // Product description ?> <div class="product-shop"> <h3 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName())?></a></h3> <?php if($_product->getRatingSummary()): ?> <?php echo $this->getReviewsSummaryHtml($_product) ?> <?php endif; ?> <?php echo $this->getPriceHtml($_product, true) ?> <?php if($_product->isSaleable()): ?> <p><button class="button" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><span><?php echo $this->__('Add to Cart') ?></span></span></span></button></p> <?php else: ?> <?php if ($userIsloggedIn) { ?> <div class="out-of-stock"><?php echo $this->__('Availability: Out of stock.') ?></div> <?php } else { ?> <div class="out-of-stock"><a href="/customer/account/login/">Identificate para poder ver el precio</a></div> <?php } ?> <!-- <p class="availability"><span class="out-of-stock"><?php echo $this->__('Out of stock') ?></span>--> <?php endif; ?> <div class="clear"></div> </div> <div class="clear"></div> <div class="desc std"> <?php echo nl2br($_product->getShortDescription()) ?> <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->__('Learn More') ?></a> </div> <ul class="add-to-links"> <?php if ($this->helper('wishlist')->isAllow()) : ?> <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_product) ?>"><?php echo $this->__('Add to Wishlist') ?></a></li> <?php endif; ?> <?php if($_compareUrl=$this->getAddToCompareUrl($_product)): ?> <li class="last"><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>"><?php echo $this->__('Add to Compare') ?></a></li> <?php endif; ?> </ul> </li> <?php $list_item++; endforeach; ?> </ol> <script type="text/javascript">decorateList('products-list', 'none-recursive')</script> <?php else: ?> <?php // Grid Mode ?> <?php $_collectionSize = $_productCollection->count() ?> <table class="products-grid" id="products-grid-table"> <?php $_columnCount = 3/*$this->getColumnCount()*/; ?> <?php $_items = $_productCollection->getItems(); shuffle($_items); ?> <? // Show 3 items $max_items = 3; $personal_count = 1; ?> <?php $i=0; foreach ($_items as $_product): ?> <?php if ($i++%$_columnCount==0): ?> <tr> <?php endif ?> <td> <h3 class="product-name"><a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"><?php echo $this->htmlEscape($_product->getName()) ?></a></h3> <a class="product-image" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>"> <img src="<?php echo $this-/>helper('catalog/image')->init($_product, 'small_image')->resize(205, 181); ?>" width="205" height="181" alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" /> </a> <?php if($_product->getRatingSummary()): ?> <?php echo $this->getReviewsSummaryHtml($_product, 'short') ?> <?php endif; ?> <?php echo $this->getPriceHtml($_product, true) ?> <?php if($_product->isSaleable()): ?> <button type="button" class="button" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><span><?php echo $this->__('Add to Cart') ?></span></span></span></button> <?php else: ?> <?php if ($userIsloggedIn) { ?> <div class="out-of-stock"><?php echo $this->__('Availability: Out of stock.') ?></div> <?php } else { ?> <div class="out-of-stock"><a href="/customer/account/login/">Identificate para poder ver el precio</a></div> <?php } ?> <!-- <p class="availability"><span class="out-of-stock"><?php echo $this->__('Out of stock') ?></span>--> <?php endif; ?> <div class="clear"></div> </td> <?php if ($i%$_columnCount==0 && $i!=$_collectionSize): ?> </tr> <?php endif ?> <? if($personal_count == $max_items){break;} else{$personal_count++;} ?> <?php endforeach ?> <?php for($i;$i%$_columnCount!=0;$i++): ?> <td class="empty">Β </td> <?php endfor ?> <?php if ($i%$_columnCount==0): ?> <?php endif ?> </table> <script type="text/javascript">decorateTable('products-grid-table')</script> <?php endif; ?> </div> </div> <?php endif; ?>

Questions

Ask any questions in the comments and we can try to solve it πŸ˜‰

Forum

Remember we have a Magento Forum section for any other Magento related questions πŸ˜‰

13 comments
  • Hi there,
    We put together an alternative method which may be of interest to some:
    http://dx3webs.com/front/2010/10/how-to-create-a-random-featured-product-list-on-home-page-in-magento/

  • Thanks but the simple rick is to add these lines to the product collection

    $collection->getSelect()->order(‘rand()’);

    That will do πŸ˜‰

  • Your solution isn’t working neither blog owner or Jerome Dennis…

  • Running perfectly on Magento 1.5.1. Thanks for your code!

  • Thank you for the code!

    I have got another code from a site, then modified 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:

    <?php $_productCollection=$this->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; ?> <?php if(!$_productCollection->count()): ?> <div class="note-msg"> <?php echo $this->__('There are no products matching the selection. Please provide a category ID.') ?> </div> <?php else: ?> <ul> <?php $_collectionSize = $_productCollection->count() ?> <?php foreach ($_productCollection as $_product): ?> <li> <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>"> <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(105, 105); ?>" width="105" height="105" alt="<?php echo $this->htmlEscape($_product->getName()) ?>" /> </a> </li> <?php endforeach ?> </ul> <?php endif; ?>

    This code displays a single product.

  • Thanks a lot for sharing this!

  • Why not use Magento’s built in functionality for this?

    <p>{{block type="catalog/product_list_random" category_id="3" template="catalog/product/list.phtml" columnCount="5"}}</p>
  • $collection->getSelect()->order('rand()'); wasn’t working

    Many thanks for the shuffle hint

  • Hi! Nice Tut. How do we limit the product display to only one? Thanks

Leave a Reply

Add <code> Some Code </code> by using this tags.

*
*