Determine if on product page programmatically in Magento(在 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;
}
希望有帮助!
谢谢,乔
这篇关于在 Magento 中以编程方式确定是否在产品页面上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 Magento 中以编程方式确定是否在产品页面上


- 如何从数据库中获取数据以在 laravel 中查看页面? 2022-01-01
- 如何在 Symfony2 中正确使用 webSockets 2021-01-01
- 如何使用 Google API 在团队云端硬盘中创建文件夹? 2022-01-01
- PHP - if 语句中的倒序 2021-01-01
- 使用 GD 和 libjpeg 支持编译 PHP 2022-01-01
- PHP foreach() 与数组中的数组? 2022-01-01
- 覆盖 Magento 社区模块控制器的问题 2022-01-01
- Laravel 5:Model.php 中的 MassAssignmentException 2021-01-01
- openssl_digest vs hash vs hash_hmac?盐与盐的区别HMAC? 2022-01-01
- Oracle 即时客户端 DYLD_LIBRARY_PATH 错误 2022-01-01