Display Random Product On Home Page – Magento

Aug 19, 2013, by admin

In this post we are going to see how to display random product on homepage in magento

To display random products on your store’s home page in Magento Theme you need to follow the steps given below

Create a new file called random.phtml at app/design/frontend/default/Your_Theme/template/catalog/random.phtml then add the following code in that file

<?php
$chosen_category = Mage::getModel(‘catalog/category’)->load($this->getCategoryId());
$_productCollection = $this->getLoadedProductCollection();
$number_of_products = $this->getNumProducts();
if (sizeof($_productCollection) < $number_of_products) {
    $number_of_products = sizeof($_productCollection);
}
$displayed_products = array();
foreach ($_productCollection as $_product) {
    $displayed_products[] = $_product;
}
$random_products = array();
if (sizeof($_productCollection) > 1) {
    $random_products = array_rand($displayed_products, $number_of_products);
} else {
    $random_products = array(‘0’);
}
?>
<?php if(!$_productCollection->getSize()):?>
<div>
    <?=$this->__(‘There are no products matching the selection.’)?>
</div>
<?php else:?>
<div>
  <div>
  <div>
      <h2>Random Products</h2>
  </div>
  <div>
    <table id=”products-grid-table”>
    <?php
    $k=0;
    for ($i=0; $i < $number_of_products; $i++): ?>
    <?php if ($k == 0) { ?>
            <tr>
        <?php } if($k==3)    { $k=0;  ?>
            </tr><tr>
        <?php } ?>
            <td id=”td_<?php echo ($k+1); ?>” <?php if($k==3){ ?>class=”last”<? } ?> >
        <div>
              <div id=”cont_<?php echo ($k+1); ?>”>
              <div>
                  <?php
                  $pname=$this->htmlEscape($displayed_products[$random_products[$i]]->getName());
                  ?>
                  <h3>
                  <a href=”<?php echo $displayed_products[$random_products[$i]]->getProductUrl()?>” title=”<?php echo $pname; ?>”>
                    <?php if(strlen($pname) > 28) {echo substr($pname,0,25).”…”;}else {echo $pname;}?>
                    </a></h3>
                </div>
                <div>
                <a href=”<?php echo $displayed_products[$random_products[$i]]->getProductUrl()?>”> <img src=”<?php echo $this->helper(‘catalog/image’)->init($displayed_products[$random_products[$i]], ‘small_image’)->resize(140);?>” alt=”<?php echo $this->htmlEscape($displayed_products[$random_products[$i]]->getName())?>” title=”<?php echo $this->htmlEscape($displayed_products[$random_products[$i]]->getName())?>”/> </a>
                </div>
                <div style=” text-align:center;”>
                    <span id=”product-price-37″>
                        <span ><?php echo Mage::app()->getLocale()->currency(Mage::app()->getStore()->
     getCurrentCurrencyCode())->getSymbol().$displayed_products[$random_products[$i]]->getPrice(); ?></span>
                    </span>
                </div>
                <div style=”text-align:center;”>
                            <button onclick=”setLocation(‘<?php echo $displayed_products[$random_products[$i]]->getProductUrl();?>’)” type=”button”><span><span><span>Details</span></span></span></button>
                    <button onclick=”setLocation(‘<?php echo $this->getUrl(”).”/checkout/cart/add?product=”.$displayed_products[$random_products[$i]]->getId().”&qty=1″  ?>’)”><span><?php echo $this->__(‘Add to Cart’) ?></span></button>
                </div>
              </div>
        </div></td>
<?php $k++; ?>
<?php endfor;?>
</tr>
      </table>
    </div>
  </div>
</div>
<?php endif;?>

Hope this post will be useful to get more updates like the page Bugtreat Technologies

 

      Thanks to http://blog.magikcommerce.com/