如何在 magento 中获取当前产品的类别名称(在产品详细信息页面上)

how to get category name of current product (on product detail page) in magento(如何在 magento 中获取当前产品的类别名称(在产品详细信息页面上))
本文介绍了如何在 magento 中获取当前产品的类别名称(在产品详细信息页面上)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我使用了以下代码,但不适用于这种情况:

$_category_detail=Mage::registry('current_category');echo $_category_detail->getName();

得到致命错误:在/app/design/frontend/base/default/template/catalog/product/view.phtml中的非对象上调用成员函数getName()>

我们制作了一些过滤器并在 head.phtml 中使用以下提及的代码:

$is_product = Mage::registry('product');如果($is_product){if(is_object(Mage::registry('current_category'))){$category_name = Mage::registry('current_category')->getName();}else{ $category_name = "";}}

但这仅在您从类别转到产品时才有效.如果您直接访问产品页面,则不会显示任何内容

解决方案

这是因为产品可以附加到多个类别.在您的情况下,当您访问从类别页面引用的产品页面时,您的会话具有类别信息.但是如果您直接访问产品页面,Magento 无法知道您来自哪个类别,因此无法显示特定类别,因为您的产品可以有多个类别.

但在您的情况下,如果您的产品只附加一个类别,您可以使用此代码,它显示产品的第一个类别名称;

 $categoryIds = $_product->getCategoryIds();如果(计数($categoryIds)){$firstCategoryId = $categoryIds[0];$_category = Mage::getModel('catalog/category')->load($firstCategoryId);echo $_category->getName();}

I used following codes but didn't work for this case:

$_category_detail=Mage::registry('current_category');
echo $_category_detail->getName();

got Fatal error: Call to a member function getName() on a non-object in /app/design/frontend/base/default/template/catalog/product/view.phtml

we make some filters and use below mention code in head.phtml:

$is_product = Mage::registry('product');

if($is_product){ 

  if(is_object(Mage::registry('current_category'))){ 
    $category_name = Mage::registry('current_category')->getName(); 
  }
  else{ $category_name = ""; }

}

But this only works if you go from a category to a product. If you visit the product page directly nothing is being displayed

解决方案

It's because products can be attached to multiple categories. In your situation, when you visit a product page referred from a category page, your session has category information. But if you visit directly product page, Magento can not know which category you came from, so it can not show you a specific category, because your product can have multiple categories.

But in your situation, if your products are attached only one category, you can use this code, it shows first category name of product;

        $categoryIds = $_product->getCategoryIds();

        if(count($categoryIds) ){
            $firstCategoryId = $categoryIds[0];
            $_category = Mage::getModel('catalog/category')->load($firstCategoryId);

            echo $_category->getName();
        }

这篇关于如何在 magento 中获取当前产品的类别名称(在产品详细信息页面上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

In PHP how can you clear a WSDL cache?(在 PHP 中如何清除 WSDL 缓存?)
failed to open stream: HTTP wrapper does not support writeable connections(无法打开流:HTTP 包装器不支持可写连接)
Stop caching for PHP 5.5.3 in MAMP(在 MAMP 中停止缓存 PHP 5.5.3)
Caching HTTP responses when they are dynamically created by PHP(缓存由 PHP 动态创建的 HTTP 响应)
Memcached vs APC which one should I choose?(Memcached 与 APC 我应该选择哪一个?)
What is causing quot;Unable to allocate memory for poolquot; in PHP?(是什么导致“无法为池分配内存?在 PHP 中?)