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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3503842/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:00:54  来源:igfitidea点击:

Magento: How to tell if you're on a category page, or product page in .phtml file(s)

magentothemesphp

提问by Jeff

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.

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

For example this code:

例如这段代码:

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

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

每当我在 CMS 页面以外的页面上时,返回“目录”。

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!

任何帮助将不胜感激!

回答by Alan Storm

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.

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

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.

如果尚未设置对象(即您在没有类别或产品的页面上),则要么返回 null,要么返回类别和产品对象。

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

如果没有返回产品对象但返回了类别对象,则您在类别页面上。类别对象具有获取父 ID 的方法

$category->getParentId()

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

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

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 notrely on the contents of the registry alone to determine the page I'm on. Instead I'd use the fullaction namein combination with looking for the above the objects.

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

回答by Jonathan Day

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:

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

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

That will return categoryor productbased on their controllers being CategoryController.phpand ProductController.phprespectively.

这将返回categoryproduct基于它们的控制器分别是CategoryController.phpProductController.php

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

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

回答by Mayers

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:

我不太喜欢检查 current_category 注册表是否存在,因为基本上任何控制器都可以做到这一点,但这并不一定意味着它是一个类别。我这样做的方式更健壮一点:

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

回答by silvo

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").

恐怕你试图以错误的方式去做。我可能是错的,因为你还没有解释你想要实现的究竟是什么,但我会使用布局 xml 将你的块包含在带有参数的产品页面上(比如 product-page="1"),类似地在类别页面上 (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.

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