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.

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.




isLoggedIn(); ?>
getLoadedProductCollection() ?>
count()): ?>

__('There are no products matching the selection.') ?>

 

getMode()!='grid'): ?>




  1. <?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>

    htmlEscape($_product->getName())?>

    getRatingSummary()): ?>
    getReviewsSummaryHtml($_product) ?>

    getPriceHtml($_product, true) ?>
    isSaleable()): ?>


    __('Availability: Out of stock.') ?>


    getShortDescription()) ?>
    __('Learn More') ?>

count() ?>

getColumnCount()*/; ?>

getItems();
shuffle($_items);
?>








htmlEscape($_product->getName()) ?>


<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>

getRatingSummary()): ?>
getReviewsSummaryHtml($_product, 'short') ?>

getPriceHtml($_product, true) ?>
isSaleable()): ?>

__('Availability: Out of stock.') ?>



 


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

About Rick

Senior Front-end Software Engineer from Barcelona, Haidong Gumdo Instructor (korean martial art of the sword), street photographer, travel lover, TV addict, Boston Red Sox fan, and privacy advocate.
13 comments

Leave a Reply

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

*
*