How to get the category name in product/view.phtml from product_viewed.phtml?

偶尔善良 提交于 2019-12-13 03:36:58

问题


How can I get the category name from product_viewed.phtml list?

I get an error when I land on the product page frontend\base\default\template\catalog/product/view.phtml

if I click a product from frontend\base\default\template\reports/product_viewed.phtml

This is the code on my view.phtml

<?php
$_helper = $this->helper('catalog/output');
$_category_detail = Mage::registry('current_category');
var_dump($_category_detail->getName()); //gives current  category name
?>

This is the error I get,

Fatal error: Call to a member function getName() on a non-object in C:\xxx\frontend\base\default\template\catalog\product\view.phtml on line 86

But I don't have this error, if I click the product from frontend\base\default\template\catalog/product/list.phtml

Any idea how can I get the category name from these two different situations?


回答1:


I think you can get the category id from product name or id by loading model of product.

such as following code.

  $catCollection = $_product->getCategoryCollection();
    foreach($catCollection as $cat){
      print_r($cat->getData());
      //echo $cat->getName();
      //echo $cat->getUrl();
    }

Another Way,

$_product= Mage::getModel('catalog/product');
$catIds = $_product->getCategoryIds();

After that you can get the categories by accessing $catIds array such as $catIds[0], $catIds[1] etc.




回答2:


You can't get Mage::registry('current_category') on catalog/product/view.phtml if you access a product with direct link. But if you follow the proper channel like, click on any category then select a product, this particular category will get registered. You should use the following code where ever you are displaying the categories name:

     if(Mage::registry('current_category')){
        echo Mage::registry('current_category')->getName();
     }else{
        $categories = $_product->getCategoryCollection()
                          ->addAttributeToSelect('name');
        foreach($categories as $category) {
            echo $category->getName();
        }
     }



回答3:


tealou,

Mage::registry('current_category') depends on Product url if product url have category id in this routing url ,then you get Mage::registry('current_category'),else you did not get Mage::registry('current_category') value. It is logic of url_rewrite feature.

Suppose you have product testing(id 235 ) and it categories catone(id 5),cattwo(id 7),catthree (id 11)

for product url

testing.html and target path is catalog/product/view/id/235/

catone/testing.html and target path is catalog/product/view/id/235/category/5
cattwo/testing.html and  target path is catalog/product/view/id/235/category/7
catthree /testing.html and  target path is catalog/product/view/id/235/category/11

First url not have categoryid so if you click on this url from any category page is not give any value Mage::registry('current_category') Other have category it will give Mage::registry('current_category') value in view.phtml of product page.

Goto Admin>Catalog>Url rewrite manager her you are find this url

If want to Mage::registry('current_category') then you need to create a new Mage::registry() varible from /catalog/category/view.phtml i.e category page.

$var=Mage::registry('current_category');

Mage::register('some_name', $var);

and fetch in catalog/product/view.phtml using

$my_var = Mage::registry('some_name');

This some_name variable will get when if you will come to product page from category. put code like

  if(Mage::registry('some_name')){
    var_dump(Mage::registry('some_name'))->getName()); 
//gives current  category name
    }



回答4:


You can not get current category on product page b\c one product can have many categories. What if customer comes to your product page from google.com, for example? What category will this be?

There is no such Mage::registry('current_category') variable on product page. Forget it. This variable exists only on category pages. On product page you can only check if product is in some category.




回答5:


almost as MTM has been said, I use this:

if(Mage::registry('current_category')){
    $catid = Mage::registry('current_category')->getId();
    $cat = Mage::getModel('catalog/category')->load($catid);
} else {
    //because sometimes we can not use registry('current_category'), so we use:
    $cat_ids = $_product->getCategoryIds(); // get all categories where the product is located
    $cat = Mage::getModel('catalog/category')->load( $cat_ids[0] ); // load first category
}



回答6:


Here is the code i use in Magento 1.8

<?php $cats = $_product->getCategoryIds();
     foreach ($cats as $category_id) { $_cat = Mage::getModel('catalog/category')->load($category_id) ;
     echo $_cat->getName();
}?>


来源:https://stackoverflow.com/questions/24520042/how-to-get-the-category-name-in-product-view-phtml-from-product-viewed-phtml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!