Which page is this? – Magento

By Avinash

August 02, 2012Tips & Tricks2 Comments

Finally Article on Magento after very long time. Last article which I have posted about magento is about Magento EAV Database structure. In that article I have explained the reason for the flexibility Magento.

In this article I will talk about some tips and tricks which will be much helpful when you are dealing with the Theme Integration.

Reason For this Article

In this article I am going to share tips for Magento with which we can identify the page on which we are; just like home page, category page, product page, etc.

Main to reason for this article is that; it will be very much helpful when you want to load some content conditionally. Like some js files need to be loaded on certain pages only. Yes we can manage this using XML file as well in magento but I am just taking this as an example. But the thing is below conditions will be useful when you really need it.

So let’s start with conditions for different pages. We can use this code in .phtml file.

Detect Home Page – Magento

Method 1 (For All Version of Magento):

if($this->getUrl('') == $this->getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true))) { // Home page }
if($this->getUrl('') == $this->getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true)))
{
  // Home page
}

Method 2 (For Magento 1.5 and Above):

if($this->getIsHomePage()) { // Home page }
if($this->getIsHomePage())
{
  // Home page
}

Detect Category Page – Magento

if (Mage::registry('current_category')) { // category page }
if (Mage::registry('current_category'))
{
  // category page
}

Now see how we can get ID and Name of the category if current page is category page.

if (Mage::registry('current_category')) { // Category Name echo Mage::registry('current_category')->getName(); // Category ID echo Mage::registry('current_category')->getId(); }
if (Mage::registry('current_category'))
{
  // Category Name
  echo Mage::registry('current_category')->getName();

  // Category ID
  echo Mage::registry('current_category')->getId();
}

Detect CMS Page – Magento

if(Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms') { // CMS page }
if(Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms')
{
  // CMS page
}

Get CMS page name if current one is the CMS page.

if(Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms') { echo Mage::getSingleton('cms/page')->getIdentifier(); }
if(Mage::app()->getFrontController()->getRequest()->getRouteName() == 'cms')
{
  echo Mage::getSingleton('cms/page')->getIdentifier();
}

Detect Product Detail Page – Magento

if(Mage::registry('current_product')) { // Product detail page }
if(Mage::registry('current_product'))
{
  // Product detail page
}

Detect Configure Product Page – Magento

if(Mage::app()->getFrontController()->getRequest()->getRequestedActionName() == 'configure') { // Product Configuration page }
if(Mage::app()->getFrontController()->getRequest()->getRequestedActionName() == 'configure')
{
  // Product Configuration page
}

Detect Cart Page – Magento

$request = $this->getRequest(); $module = $request->getModuleName(); $controller = $request->getControllerName(); $action = $request->getActionName(); if($module == 'checkout' && $controller == 'cart' && $action == 'index') { //Cart Page }
 $request = $this->getRequest();
 $module = $request->getModuleName();
 $controller = $request->getControllerName();
 $action = $request->getActionName();
 if($module == 'checkout' && $controller == 'cart' && $action == 'index')
 {
   //Cart Page
 }

I have found above code snippets very useful when I used to work with the Magento. What about you? Consider sharing this article if you find this useful. Let me know your reaction by commenting here.

If you don’t want to miss any upcoming article then subscribe to our RSS Feed via mail, Like us on Facebook or Follow us on Twitter. If you do so, I am sure you will never miss any article/freebies or updates from us.

Share This Article

Tags:
  • Pingback: 4 Ways to Get Magento Homepage URL | Expert PHP Developer

  • shirtsofholland

    Method 2 does not work in magento 8.1

    We use

    $route = Mage::app()->getFrontController()->getRequest()->getRouteName();

    $action = Mage::app()->getFrontController()->getRequest()->getActionName();

    if($route == ‘cms’ && $action == ‘index’):

    echo $this->getChildHtml(‘shopper_footer_partners’);

    endif;