确定是否在Magento中以编程方式在产品页面上 [英] Determine if on product page programmatically in Magento
问题描述
我想在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",但对于类别浏览页面和产品查看页面都返回目录",因此我不能用它来区分类别页面和产品页面.
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中以编程方式在产品页面上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!