Determine if on product page programmatically in Magento

后端 未结 4 1298
囚心锁ツ
囚心锁ツ 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: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.

提交回复
热议问题