Friday, May 17, 2013

Magento - Check Current page is Category | DETERMINE IF CURRENT PAGE IS A CATEGORY

After a great deal of frustration and numerous tantrums I discovered the solution is to wrap the code in a loop that checks if it’s a category page.
//LOOP TO CHECK IF ITS A CATEGORY PAGE //INNER LOOP getId() == 5):?>
Shows this paragraph is the category id="5"
Else show this for different category pages
> //SHOW THIS CONTENT IF ITS A CMS PAGE
Else show this paragraph for all other pages
>
Posted in All, News, Tutorials and tagged eCommerce, Magento, Magento Category, Magento current_category, Mangento If Category.

========================

 Magento Determine if Current Page is Category or Not

Sometimes you find your self in the need to determine if the current page a visitor is viewing is of a category or not. The reason for this could drastically very, but one good reason is the need for a global element not be shown on any category page.
It would be much faster to simply include this element or rather custom block globally to your Magento store via your layout XML files, and have a condition in this block’s .phtml file that excluded any output on category page.
This is a very simple method that simply looks to our Mage registry and asks the question “Is the current page indeed a current category page or not?”.

<?php if(!Mage::registry('current_category')):?>
//OUTPUT THE GLOBAL ELEMENTS CODE ON NONE-CATEGORY PAGES
php endif; ?>
 
 
Simple as that, of course the correct way would be to include this block globally via your XML layout files, and then remove it via XML (Custom Layout Update) in the backend->catalog->manage categories “Custom Design” tab for each category.



============================

Get the current category in Magento

 

I’ve come across several places where I’ve needed to get the current category (or information from it) in one way or another. Some blocks have the ability to do this:
$_category = $this->getCurrentCategory();
However, if you need to get the current category in a block (or model, helper, whichever) where this method isn’t present, then we can simply do this instead:
$_category = Mage::registry('current_category');
They both return exactly the same object (unless overridden, that is Mage_Catalog_Model_Category).
Caveat: If you are in a block that doesn’t have access to ‘getCurrentCategory’ nor is Mage::registry(‘current_category’) defined (for example, the product list being applied to the home/front page) you can load by category ID if known. In this example, the Mage_Catalog_Block_Product_List has been applied to the front page, so you’d need to do this instead:
// Check for $_category's existance/status after initial block load, and grab the category ID from the object's parameters/attributes.
// Replace $this->getCategoryId() with a category ID, whether defined statically or retrieved from another method.
if (!is_object($_category))
     $_category = Mage::getModel('catalog/category')->load($this->getCategoryId());
Of course, this ends up being slightly different depending on where you need to get the category object.

By PHP with No comments

0 comments:

Post a Comment

    • Popular
    • Categories
    • Archives