Magento Auto Redirect To Product Details Page If Category Have Only One Product

Share

A very important step, a successful eCommerce site must take, is to make sure the customer has to go through minimum pageloads. For example if there is only one product under a category, there is no use in loading the category page (unless you want your customers to read the category details, which is needed in some cases). The customer should be taken directly to the product details page.

Today let us see how we can achieve this for Magento.

1. Open the page template which is assigned to the category pages. You will find them under “template/page/” folder in your theme directory. For example let us assume the page is 1column.phtml (for the One column layout).

2. Just after the lines

 
/**
 * Template for Mage_Page_Block_Html
 */

Add the following

 
$product = Mage::registry('current_product');  
if($product == ''){
	$category = Mage::registry('current_category');
	if(is_object($category)){
		$catLoaded = Mage::getModel('catalog/category')->load($category->getEntityId());
		$collection = $catLoaded->getProductCollection();
		$collection->addAttributeToSelect('*');
		if(count($collection) == 1){
			foreach($collection as $product){
				$productUrl = $product->getProductUrl();
				header("location:$productUrl");
				exit;
			}
		}
	}
}

That should be enough to redirect all category pages, which have only one product under them, to the product details page.

This entry was posted in Magento and tagged , , , . Bookmark the permalink.
 

5 Responses to Magento Auto Redirect To Product Details Page If Category Have Only One Product

  1. Rasel Rony says:

    Thanks a lot. I was looking for a redirection solution like this on my Magento site. And you know your codes worked like a charm.

  2. Sven says:

    Thanks bro! Works nice!!!

  3. jeeva says:

    Thank you buddy, i need to try this solution last one week.

  4. Aaron says:

    For performance reasons, rather than selecting all of the attributes, just choose one. I went with ‘entity_id’. That’s like doing a SELECT * in MySQL when you only need one column.

  5. Aaron says:

    I forgot to say thanks though, just what I was looking for without an extension 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

*