or send us a message

Magento: How to detect current page type in phtml

Written by: Tom Dollar, On: Monday 1st December 2014

Often when you are working on custom functionality in your Magento store, you need to know what the current type of page being viewed is.

For example, you may be working on a header module which appears on every page of your store, but you want to show specific content to highlight a delivery offer on just the product pages. Luckily, this is quite straight forward in Magento.

Magento registers the frontend controller action for most page types within the application, which means we can hook into this and identify them too. This could be a product page view, a category page view, cms page, checkout index or specific module view.

To add additional content on a product page only, use this code;


$pageIdentifier = Mage::app()->getFrontController()->getAction()->getFullActionName();
if($pageIdentifier == 'catalog_product_view') { 
    // Do something on product view page only
    echo 'This is a product view page';
}


Struggling to find the FullActionName you're after? Temporarily add the following code to either the header or footer of your site;


// TO BE REMOVED
$pageIdentifier = Mage::app()->getFrontController()->getAction()->getFullActionName();
Mage::log($pageIdentifier,null,'events.log');


With this code in place, flush your cache to ensure the latest version of the file is read, and take a surf around your site. Each frontend action name you trigger will now be logged in the following location:

[MAGENTO ROOT]/var/log/events.log


2014-12-05T20:04:25+00:00 DEBUG (7): catalog_category_view
2014-12-05T20:04:30+00:00 DEBUG (7): cms_page_view
2014-12-05T20:04:48+00:00 DEBUG (7): customer_account_login
2014-12-05T20:04:50+00:00 DEBUG (7): checkout_cart_index
2014-12-05T20:05:09+00:00 DEBUG (7): cms_index_index
2014-12-05T20:05:14+00:00 DEBUG (7): catalog_category_view
2014-12-05T20:05:17+00:00 DEBUG (7): catalog_product_view
2014-12-05T20:05:23+00:00 DEBUG (7): checkout_cart_index
2014-12-05T20:05:28+00:00 DEBUG (7): customer_account_login
2014-12-05T20:05:31+00:00 DEBUG (7): checkout_cart_index


Once you've found what you're looking for, make absolutely sure you remove the logging script as this will fill up your log incredibly fast!

Pixie Media

We are a Devon web design, ecommerce and branding agency, specialising in Magento eCommerce. With over 15 years experience, we offer complete digital solutions to clients across the globe.

We Specialise In;