Tuesday, 3 July 2012

How to display random products on home page in Magento

To display random products on home page in Magento Theme you need to follow the below give steps:-
Step 1.
Create a new file called random.phtml at app/design/frontend/default/Your_Theme/template/catalog/random.phtml with the following given code:-



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
 
$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');
}
?>
 if(!$_productCollection->getSize()):?>
<div class="note-msg">
    $this->__('There are no products matching the selection.')?>
div>
 else:?>
 
<div class="main-binder">
  <div class="cms-box">
  <div class="category-title">
      <h2>Random Productsh2>
  div> 
  <div class="category-products">
    <table id="products-grid-table" class="products-grid">
  
 $k=0;
 for ($i=0; $i < $number_of_products; $i++): ?>
  if ($k == 0) { ?>
   <tr class="first odd">
   } if($k==3) { $k=0;  ?> 
   tr><tr class="first odd even">
   } ?>
         <td id="td_$k+1); ?>"  if($k==3){ ?>class="last" } ?> >
  <div class="cms-box">
              <div id="cont_$k+1); ?>">
     <div class="product-name-block">                 
      
      $pname=$this->htmlEscape($displayed_products[$random_products[$i]]->getName());
      ?>
      <h3 class="product-name">      
      <a href="$displayed_products[$random_products[$i]]->getProductUrl()?>" title="$pname; ?>">
                     if(strlen($pname) > 28) {echo substr($pname,0,25)."...";}else {echo $pname;}?>
                    a>h3>
                div>
                <div class="image-box">     
    <a class="product-image" href="$displayed_products[$random_products[$i]]->getProductUrl()?>"> <img src="$this->helper('catalog/image')->init($displayed_products[$random_products[$i]], 'small_image')->resize(140);?>" alt="$this->htmlEscape($displayed_products[$random_products[$i]]->getName())?>" title="$this->htmlEscape($displayed_products[$random_products[$i]]->getName())?>"/> a> 
    div>
 
    <div class="cms-price-box" style=" text-align:center;">
     <span class="regular-price" id="product-price-37">      
      <span class="price" >php echo Mage::app()->getLocale()->currency(Mage::app()->getStore()->
     getCurrentCurrencyCode())->getSymbol().$displayed_products[$random_products[$i]]->getPrice(); ?>span>
     span>                      
    div>
    <div class="button-row" style="text-align:center;">
       <button onclick="setLocation('$displayed_products[$random_products[$i]]->getProductUrl();?>')" class="button" type="button"><span><span><span>Detailsspan>span>span>button>                               
     <button onclick="setLocation('$this->getUrl('')."/checkout/cart/add?product=".$displayed_products[$random_products[$i]]->getId()."&qty=1"  ?>')" class="button"><span>php echo $this->__('Add to Cart') ?>span>button>
    div>
              div>
        div>td> 
 
 $k++; ?>
 endfor;?>
tr>
      table>     
    div>
  div>
div>  
 endif;?>
 
 
 Note: Use this blog in magento admin.
 
<p>{{block type="catalog/product_list" category_id="3" num_products="8" template="catalog/random.phtml"}}p>
 
 Note: Category Id will be specific to your Store. 

No comments: