Determine if on product page programmatically in Magento

后端 未结 4 1280
囚心锁ツ
囚心锁ツ 2020-12-24 09:44

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 v

相关标签:
4条回答
  • 2020-12-24 10:16

    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.

    0 讨论(0)
  • 2020-12-24 10:16

    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 讨论(0)
  • 2020-12-24 10:30

    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.

    0 讨论(0)
  • 2020-12-24 10:31

    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

    0 讨论(0)
提交回复
热议问题