Magento:如何判断您是否在.phtml文件中的类别页面或产品页面上 [英] Magento: How to tell if you're on a category page, or product page in .phtml file(s)

查看:96
本文介绍了Magento:如何判断您是否在.phtml文件中的类别页面或产品页面上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果来宾在类别列表页面或产品页面上,我试图将if语句编程到我的.phtml文件中.

I am trying to program into my .phtml files an if statement if the guest is on a category list page, or on a product page.

例如此代码:

<?= Mage::app()->getFrontController()->getRequest()->getRouteName(); ?>

每当我在CMS页面以外的页面上时,都会返回目录".

Returns "catalog" whenever l'm on a page other than a CMS page.

有没有一种方法可以使用类似的方法来知道用户是在查看根类别,子类别还是单个产品页面?

Is there a way l can use a similar method to know if the user is looking at a root category, sub category or an individual product page?

任何帮助将不胜感激!

推荐答案

自从我处理前端目录页面以来已经有一段时间了,但是请尝试一下.

It's been a while since I've dealt with frontend catalog pages, but give this a try.

当前版本的Magento在某些页面上注册某些全局变量(不是PHP全局变量,而是Magento系统的全局变量).

Current versions of Magento register certain global variables (not PHP globals, but things global to the Magento system) on certain pages.

致电以下

$category = Mage::registry('current_category');         
$product  = Mage::registry('current_product');
$product  = Mage::registry('product');

如果未设置对象(即您所在的页面上没有类别或产品),

将返回null,或者返回类别和产品对象.

will either return null if the objects haven't been set (i.e. you're on a page without a category or a product), or return category and product objects.

如果返回产品对象,那么您将进入产品页面.

If a product object is returned you're on a product page.

如果未返回任何产品对象,但返回了类别对象,则您位于类别页面上.类别对象具有获取父ID的方法

If no product object is returned but a category object is, you're on a category page. Category objects have a method to getting the parent id

$category->getParentId()

没有父ID的类别应该是顶级类别,有父ID的类别应该是子类别.

A category without a parent id should be a top level category, categories with parent ids should be sub-categories.

这应该为您提供识别当前请求的位置的条件.

That should give you what you need to identify where the current request is.

更新:近十年后,我可能会仅依靠注册表的内容来确定我所在的页面.相反,我会使用

UPDATE: Coming back to this almost a decade later -- I likely would not rely on the contents of the registry alone to determine the page I'm on. Instead I'd use the full action name in combination with looking for the above the objects.

这篇关于Magento:如何判断您是否在.phtml文件中的类别页面或产品页面上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆