I want to insert tracking codes on all of the pages of a Magento site, and need to use a different syntax if the page is a CMS page, a category browsing page, or a product view page. I have a custom module set up with a block that inserts a generic tracking code on each page for now. From within the block, how can I distinguish between CMS pages, category pages, and product pages?
I started with:
Mage::app()->getRequest();
I can see that
Mage::app()->getRequest()->getParam('id');
returns the product or category ID on product and category pages, but doesn't distinguish between those page types.
Mage::app()->getRequest()->getRouteName();
return "cms" for CMS pages, but returns "catalog" for both category browsing and product view pages, so I can't use that to tell category and product pages apart.
Is there some indicator in the request I can use safely? Or is there a better way to accomplish my goal of different tracking codes for different page types?
Easy Steps to Display Products in Magento 2 Home PageSelect the widget type as 'Catalog Products List' and enter the basic information. In the bottom you can see the conditions to add. Select the option 'Category' and select the category you want to display in home page and click “Insert Widget” button. That's it.
The easest answer is the following:
<?php
echo $this->getRequest()->getControllerName();
if($this->getRequest()->getControllerName()=='product') //do something
if($this->getRequest()->getControllerName()=='category') //do others
?>
this is 100% the right way to do according to the MVC model, please look into the core code really understand it, and do not give the method with loading or depends on the registry method. Support mytraining.net even though I am not there.
There may be an even better way to do this using routers, but one fast way is to check the registry to see if we have a single product that we are looking at:
<?php
$onCatalog = false;
if(Mage::registry('current_product')) {
$onCatalog = true;
}
Hope that helps!
Thanks, Joe
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With