Check if current page is category, login, registration, checkout page and more – Magento 1.9

Sometimes in theme development, you will need to display a particular block code for specific page. To display the specific block of code into particular page, we can run these below code. These code can will easily fetch the module, controller, action name in phtml template file. These code only works on the phtml template file not in static blocks or cms page in admin panel.



 $request = Mage::app()->getRequest();
 $module = $request->getModuleName();
 $controller = $request->getControllerName();
 $action = $request->getActionName();
 $request = $this->getRequest();  

The code displays all the request on page. Please to try the code on your page to understand.

 $request->getModuleName(); 

The code displays the module name for example ‘catalog’.

 $request->getControllerName(); 

The code displays the controller name for example ‘product’.

 $request->getActionName(); 

The code displays the action name for example ‘view’.

If you go to the catalog.xml, you will find catalog_product_view. The catalog is equal to module, product is equal to controller and view is equal to action name. Below example can display a particular block of code in specific page.

$request = Mage::app()->getRequest();
$module = $request->getModuleName();
$controller = $request->getControllerName();
$action = $request->getActionName();

if($module == 'catalog' && $controller == 'product' && $action == 'view') {
   //Single product page
}

if($module == 'customer' && $controller == 'account' && $action == 'login') {
   //login page
}

if($module == 'checkout' && $controller == 'cart' && $action == 'index') {
   //Shopping Cart page
}

if($module == 'customer' && $controller == 'account' && $action == 'create') {
   //registration page
}

I hope this code really helps you. Thank you

One thought on “Check if current page is category, login, registration, checkout page and more – Magento 1.9

Leave a Reply

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