php 在 Magento 中以编程方式确定是否在产品页面上
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/3041510/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Determine if on product page programmatically in Magento
提问by Del F
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?
我想在 Magento 站点的所有页面上插入跟踪代码,如果页面是 CMS 页面、类别浏览页面或产品查看页面,则需要使用不同的语法。我有一个自定义模块,它设置了一个块,现在在每个页面上插入一个通用跟踪代码。从区块内部,如何区分CMS页面、分类页面和产品页面?
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.
返回产品和类别页面上的产品或类别 ID,但不区分这些页面类型。
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.
为 CMS 页面返回“cms”,但为类别浏览和产品查看页面返回“catalog”,所以我不能用它来区分类别和产品页面。
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?
我可以安全使用请求中的某些指标吗?或者有没有更好的方法来实现不同页面类型的不同跟踪代码的目标?
回答by bzhang
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.
这是100%按照MVC模型做的正确方法,请看核心代码真正理解它,不要给出加载方法或依赖注册表方法。即使我不在那里,也支持 mytraining.net。
回答by Joseph Mastey
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
谢谢,乔
回答by Matt
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.
我希望这有帮助。
回答by Lee Saferite
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'
您可以为正在使用的块设置一个参数,以指示需要哪种类型的跟踪代码。那么你只需使用布局 XML 来解决问题。您可以使用以下布局句柄以正确的参数更新您的块: CMS Pages = 'cms_page' 类别浏览 = 'catalog_category_view' 产品查看 = '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>

