开发者

Determine if on product page programmatically in Magento

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 开发者_JAVA百科different tracking codes for different page types?


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


I thought it would be worth mentioning there is a flaw to checking

Mage::registry('current_product')

This does indeed check if a product exists, but when on a review page for example, the product is also set, therefore you may need to be more specific to determine the page location.

The following check ensures we are on a product page, by checking it is using the "catalog" module, and the controller is a "product" request. When viewing a products list of reviews it's values would be "review" (module) and "list" (controller).

if($this->getRequest()->getModuleName()=='catalog' && 
$this->getRequest()->getControllerName()=='product'){
    Mage::registry('current_product');
}

I hope this helps.


You could have a parameter to the block being used to indicate what type of tracking code is needed. Then you just use the layout XML to solve the problem. You can use the following layout handles to have your block updated with the proper parameter: CMS Pages = 'cms_page' Category browsing = 'catalog_category_view' Product viewing = 'catalog_product_view'

Something like this:

<layout>
    <default>
        <reference name="before_body_end">
            <block type="mymodule/myblock" name="myblock" />
        </reference>
    </default>
    <cms_page>
        <reference name="myblock">
            <action method="setTrackingType">
                <type>cms</type>
            </action>
        </reference>
    </cms_page>
    <catalog_category_view>
        <reference name="myblock">
            <action method="setTrackingType">
                <type>category</type>
            </action>
        </reference>
    </catalog_category_view>
    <catalog_product_view>
        <reference name="myblock">
            <action method="setTrackingType">
                <type>product</type>
            </action>
        </reference>
    </catalog_product_view>
</layout>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜