在 Magento 中以编程方式确定是否在产品页面上

2021-12-19 00:00:00 php 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",但类别浏览和产品查看页面都返回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;
}

希望有帮助!

谢谢,乔

相关文章