Magento:如何判断您是否位于 .phtml 文件中的类别页面或产品页面

发布于 2024-09-14 17:06:48 字数 293 浏览 5 评论 0原文

如果访客位于类别列表页面或产品页面上,我正在尝试将 if 语句编程到我的 .phtml 文件中。

例如,以下代码:

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

每当我位于 CMS 页面以外的页面上时,都会返回“目录”。

有没有一种方法可以使用类似的方法来了解用户是否正在查看根类别、子类别或单个产品页面?

任何帮助将不胜感激!

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.

For example this code:

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

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?

Any help would be greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

爱殇璃 2024-09-21 17:06:48

我已经有一段时间没有处理前端目录页面了,但是请尝试一下。

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

则调用以下命令

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

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

将返回 null,或者返回类别和产品对象。如果返回产品对象,您将进入产品页面。

如果没有返回产品对象,但返回类别对象,则您位于类别页面。类别对象有一个方法来获取父 id

$category->getParentId()

没有父 id 的类别应该是顶级类别,具有父 id 的类别应该是子类别。

这应该可以为您提供识别当前请求所在位置所需的信息。

更新:差不多十年后回到这个问题上——我可能不会仅依靠注册表的内容来确定我所在的页面。相反,我会使用 完整操作名称与查找上述对象相结合。

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

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

Calling the following

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

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.

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()

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.

山有枢 2024-09-21 17:06:48

虽然艾伦的答案可行,但还有一个更直接的选择,而且您的代码片段实际上走在正确的轨道上...您只需要检查控制器名称而不是模块名称:

<?php Mage::app()->getFrontController()->getRequest()->getControllerName(); ?>

这将返回 category< /code> 或 product 分别基于其控制器 CategoryController.phpProductController.php

这确实假设您没有安装任何用自己的控制器重写这些控制器的第三方模块。

While Alan's answer will work, there is a more direct option, and you were actually on the right track with your code snippet... you just need to inspect the controller name rather than the module name:

<?php Mage::app()->getFrontController()->getRequest()->getControllerName(); ?>

That will return category or product based on their controllers being CategoryController.php and ProductController.php respectively.

This does assume that you've not installed any third-party modules that rewrite those controllers with their own.

忱杏 2024-09-21 17:06:48

我不太喜欢检查 current_category 注册表是否存在,因为基本上任何控制器都可以执行此操作,但这并不一定意味着它是一个类别。我的做法更加稳健:

$fullActionName = Mage::app()->getFrontController()->getAction()->getFullActionName();
if ($fullActionName == 'catalog_category_view') { 
    ...  //Category
}
elseif ($fullActionName == 'catalog_product_view') {
    ...  //Product
}

I am not a big fan of checking if the current_category registry exists, because basically any controller could do this and it wouldn't necessarily mean it's a category. My way of doing it is a bit more robust:

$fullActionName = Mage::app()->getFrontController()->getAction()->getFullActionName();
if ($fullActionName == 'catalog_category_view') { 
    ...  //Category
}
elseif ($fullActionName == 'catalog_product_view') {
    ...  //Product
}
素年丶 2024-09-21 17:06:48

恐怕你正试图以错误的方式去做。我可能是错的,因为您没有解释您想要实现的到底是什么,但我会使用布局 xml 将您的块包含在带有参数的产品页面上(例如product-page =“1”),并且类似在类别页面上(category-page="1")。

然后,您可以通过检查块内的这些参数来判断您是在产品页面还是类别页面上:

if($this->getProductPage()) {
  //this is a product page, do some stuff
}
elseif($this->getCategoryPage()) {
  //this is a category page, do some stuff
}

区分主类别页面和子类别页面可能会更困难,首先想到的是请求变量的分析,但这肯定不是最好的方法。

I am afraid you are trying to do it the wrong way. I might be wrong, because you have not explained what is it exactly that you want to achieve, but I would use the layout xml to include your block on a product page with a parameter (say product-page="1") and similiarly on a category page (category-page="1").

Then you would be able to tell if you are on a product page or category page by examining those parameters inside your block:

if($this->getProductPage()) {
  //this is a product page, do some stuff
}
elseif($this->getCategoryPage()) {
  //this is a category page, do some stuff
}

Differentiating between main and subcategory pages might be more difficult, the first thing that comes to mind is analysis of the request variables, but that is certainly not the best approach.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文