Magento random products block

In Magento we can create block with any content we need. For the first look Magento template system is unusual, because it consists of simple .xml files in wich is written where is situated file wich produce html output and where should be placed it's output. It's very easy. So let's begin. We need to create block with random products from current category and place it on products page. 

First we need to create appropriate file in directory /app/design/frontend/your_current_theme/default/template/catalog/product/. Lets name it list_random.phtml.

Let's look on its content:

 <?php $category_id =  Mage::getModel('catalog/layer')->getCurrentCategory()->getId();


$collection = Mage::getModel('catalog/category')->load($category_id)
->getProductCollection()
->addAttributeToSelect('*') // select all attributes
->addAttributeToFilter('type_id', array('eq' => 'configurable')) 
->setPageSize(6); // limit number of results returned
Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection);
$collection->getSelect()->order(new Zend_Db_Expr('RAND()'));
//$collection->getSelect()->order(new Zend_Db_Expr('RAND()')); 

?>

<?php if(!empty($collection)):?>
	<div class="block block-related">    
	   <div class="block-content">
			<?php foreach ($collection as $product) :?>
			<?php $url =  $product->getProductUrl(); 
				  $name = $product->getName();
			?>
			<div class="related_random_products">
			  <a class="product-image" href="<?php echo $url;?>">
				<img src="<?php echo $this->helper('catalog/image')->init($product, 'thumbnail')->resize(95, 155);?>" alt=" <?php echo  $this->htmlEscape($product->getName());?>" title = " <?php echo  $this->htmlEscape($product->getName());?>"/>
			  </a>
			  <div class="product-details"> 
				<a class="title" href="<?php echo $url;?>"><?php echo $name;?></a>
			  </div>
			</div>
			  
			<?php endforeach;?>
		</div>
	</div>
<?php endif;?

This code genarate simple output wich consits of random product image and its title, both of them linked to original content.

The next step is to declare our block inside catalog.xml file wich is situated in /app/design/frontend/your_current_theme/default/layout/catalog.xml.

We can put this block anywhere we need but I'll show you an example. Open catalog.xml and find this lines:

<reference name="content">

            <block type="catalog/product_view" name="product.info" template="catalog/product/view.phtml">

Then paste this code somewhere after this line.

<block type="catalog/product_list_random" name="catalog.product.list_random" as="list_random" template="catalog/product/list_random.phtml"/>

So our block is enabled and we can access to it inside file wich expose all product output /app/design/frontend/your_current_theme/default/template/catalog/product/view.phtml. More over we can get it using as=" " declaration wich you can see inside above code.

Any where inside view.phtml we can palce our block using this code:

<?php echo $this->getChildHtml('list_random');?>

So thats all, Magento templates are not very hard. Magento is very nice but continue doing Drupal because as CMS Drupal is better. Good luck! 

Rating: 
0 out of 10 based on 0 ratings.