Detect product page in Magento

Recently, we have faced a problem in a Magento project. We were implementing search functionality for products. User can enter product name choose a category and sub category to search. Now the problem was. This search box should only be displayed on category page and sub category page. On product page, it should not be visible. First we have tried to hide search box on a client side through JavaScript. However, there was an annoying flicker effect. Search box displayed for some time, and then it gets to hide. So we have decided to hide it from server side code. In Magento this search box is generated from.

Following file /app/design/frontend/base/default/template/catalogsearch/form.mini.phtml

We can hide it from this file. Now the challenge was to detect the product page. We have used following code for that.

$isProductPage=false;
if(Mage::registry(‘current_category’))
{
$cat=Mage::registry(‘current_category’);
$catLevel=$cat->getLevel();$catId=$cat->getId();  $_id= Mage::app()->getRequest()->getParam(‘id’, false);  if($_id!=$catId)//spacial condition for product page  {   $isProductPage=true;  }}


In case of category and sub category page$catId and $_id gives you thesame id. For product page $catId gives you id of productcategory while $_id gives you product id.

So this was thetrick to detectthe product page.


This entry was posted in Magento, Product Page on by .

About Hiren Dave

Hiren Dave is a software engineer, blogger and consultant with over 9 years of professional experience. He writes code in number of languages. Hiren has a passion for developing web apps, mobile apps and writing books and blog articles about programming. He has a solid experience of startups as he manages products for few startups. He is also passionate about researching and learning new technologies and trends. He has also authored a book Instant Sencha Touch, published by Packt and creator of Video Course "Beginning Laravel" for Packt

3 thoughts on “Detect product page in Magento”

  1. Anonymous

    Nice one, thank you, this helps a lot.

    However, this doesn't seem to work if I enter a product page via the upsell-box of another product in magento, that page is not identified as product page in my system.

    Reply ↓

Leave a Reply

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