問題描述
我想在 Magento 站點的所有頁面上插入跟蹤代碼,如果頁面是 CMS 頁面、類別瀏覽頁面或產品查看頁面,則需要使用不同的語法.我有一個自定義模塊,其中設置了一個塊,現在可以在每個頁面上插入通用跟蹤代碼.從區塊內部,如何區分CMS頁面、分類頁面和產品頁面?
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?
我開始于:
Mage::app()->getRequest();
我可以看到
Mage::app()->getRequest()->getParam('id');
返回產品和類別頁面上的產品或類別 ID,但不區分這些頁面類型.
returns the product or category ID on product and category pages, but doesn't distinguish between those page types.
Mage::app()->getRequest()->getRouteName();
CMS 頁面返回cms",但類別瀏覽和產品查看頁面都返回catalog",所以我不能用它來區分類別和產品頁面.
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?
推薦答案
使用路由器可能有更好的方法來做到這一點,但一種快速的方法是檢查注冊表以查看我們是否有我們正在使用的單個產品看著:
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;
}
希望有幫助!
謝謝,喬
這篇關于在 Magento 中以編程方式確定是否在產品頁面上的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!