Saturday, August 7, 2010

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.


3 comments:

  1. thanks for sharing!

    ReplyDelete
  2. nice trick.
    thank you for sharing this

    regards,
    Riki RIsnandar

    ReplyDelete
  3. 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.

    ReplyDelete