views:

33

answers:

2

Hi everyone,

I am trying to program into my .phtml files an if statement if the guest is on a category list page, or on a product page.

For example this code:

<?= Mage::app()->getFrontController()->getRequest()->getRouteName(); ?>

Returns "catalog" whenever l'm on a page other than a CMS page.

Is there a way l can use a similar method to know if the user is looking at a root category, sub category or an individual product page?

Any help would be greatly appreciated!

A: 

I am afraid you are trying to do it the wrong way. I might be wrong, because you have not explained what is it exactly that you want to achieve, but I would use the layout xml to include your block on a product page with a parameter (say product-page="1") and similiarly on a category page (category-page="1").

Then you would be able to tell if you are on a product page or category page by examining thos parameters inside your block:

if($this->getProductPage()) {
  //this is a product page, do some stuff
}
elseif($this->getCategoryPage()) {
  //this is a category page, do some stuff
}

Differentiating between main and subcategory pages might be more difficult, the first thing that comes to mind is analysis of the request variables, but that is certainly not the best approach.

silvo
+1  A: 

It's been a while since I've dealt with frontend catalog pages, but gives this a try.

Current versions of Magento register certain global variables (not PHP globals, but things global to the Magento system) on certain pages.

Calling the following

$category = Mage::registry('current_category');         
$product  = Mage::registry('current_product');
$product  = Mage::registry('product');

will either return null if the objects haven't been set (i.e. you're on a page without a category or a product), or return category and product objects.

If a product object is returned you're on a product page.

If a no product object is returned but a category object is, you're on a category page. Category objects have a method to getting the parent id

$category->getParentId()

A category without a parent id should be a top level category, categories with parent ids should be sub-categories.

That should give you what you need to identify where the current request is.

Alan Storm
That's a great idea Alan.
silvo