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; ?>