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:

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.

Break on 3

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

And then at the end:

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.

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 πŸ˜‰

Tags:

13 Responses to “How to Random Products Home Magento”

  1. Andy Bird October 4, 2010 at 12:41 am #

    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/

    • quicoto October 4, 2010 at 8:31 am #

      That solutions is also interesting.

      Regards.

  2. Jerome Dennis D September 3, 2011 at 7:58 am #

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

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

    That will do πŸ˜‰

  3. Tomas October 20, 2011 at 3:10 pm #

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

  4. Magento Ruiwant February 21, 2012 at 6:59 am #

    Running perfectly on Magento 1.5.1. Thanks for your code!

  5. tuba March 19, 2012 at 4:27 pm #

    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:
    [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; ?>
    [/code]

    This code displays a single product.

    • Rick March 19, 2012 at 4:35 pm #

      Thank you for sharing! πŸ˜‰

  6. Willian April 3, 2012 at 4:37 am #

    Thanks a lot for sharing this!

  7. jmspldnl July 3, 2012 at 9:14 pm #

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

    [code]<p>{{block type="catalog/product_list_random" category_id="3" template="catalog/product/list.phtml" columnCount="5"}}</p>[/code]

  8. John July 3, 2014 at 3:57 pm #

    $collection->getSelect()->order(β€˜rand()’); wasn’t working

    Many thanks for the shuffle hint

    • Rick July 3, 2014 at 4:00 pm #

      Glad it helped John!

  9. Renato Medina August 31, 2014 at 4:03 pm #

    There is the out of the box way to do it. Simple and native

    http://www.magentocommerce.com/wiki/groups/248/display_products_on_home_page

  10. Elai February 29, 2016 at 6:28 pm #

    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.