Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Grabbing the category name from Product View
 
amanzi
Jr. Member
 
Total Posts:  3
Joined:  2008-08-01
 

So sometimes the shopping cart will send the user to the product view page without the category attached in the ULR. I don’t know if this is something that is just expected or I did something to cause this to happen. However, I needed to get the name of the category so I used this code:

<?php echo $this->getProduct()->getCategory()->getName() ?>

this works until you try to access the page without the category in the url—which produced this result

Fatal error: Call to a member function getName() on a non-object in /usr/www/users/name/app/design/frontend/default/default/template/catalog/product/view.phtml on line 47

any thoughts or ideas on what I can do to get the category name?

 
Magento Community Magento Community
Magento Community
Magento Community
 
LeeSaferite
Guru
 
Avatar
Total Posts:  322
Joined:  2007-08-31
Lake City, FL
 

Try checking the result of $this->getProduct()->getCategory().

like

<?php echo ($this->getProduct()->getCategory() ? $this->getProduct()->getCategory()->getName() : 'no category'?>

The problem is that catalog/product/view tries to set the category on the product model if it can figure it out.  First it looks for a URL parameter to load.  Absent that, it tries to find the last view category.  If the product can possibly be in that category, it uses that.  Otherwise it doesn’t set a category, which is the cause of your fatal error.  Products can exist in multiple categories, so it doesn’t know which one to show if you do not tell it.

 
Magento Community Magento Community
Magento Community
Magento Community
 
sljacome
Jr. Member
 
Avatar
Total Posts:  19
Joined:  2008-10-07
Mexico City
 

Excelent! Thanks a lot for the help!

 
Magento Community Magento Community
Magento Community
Magento Community
 
vij
Sr. Member
 
Total Posts:  81
Joined:  2009-02-13
 

<?php $categories $_product->getCategoryIds(); ?>
            <?php 
foreach($categories as $k => $_category_id): ?>
               <?php $_category 
Mage::getModel('catalog/category')->load($_category_id?>
               
               
<div><?php echo $_category->getName() ?></div>
            
<?php endforeach; ?>
 
Magento Community Magento Community
Magento Community
Magento Community
 
vij
Sr. Member
 
Total Posts:  81
Joined:  2009-02-13
 

<?php $categories $_product->getCategoryIds(); ?>
            <?php 
foreach($categories as $k => $_category_id): ?>
               <?php $_category 
Mage::getModel('catalog/category')->load($_category_id?>
               
               
<div><?php echo $_category->getName() ?></div>
            
<?php endforeach; ?>

i think this will work.
thank u

 
Magento Community Magento Community
Magento Community
Magento Community
 
mattrock
Jr. Member
 
Avatar
Total Posts:  8
Joined:  2011-04-04
 
Lee Saferite - 23 September 2008 12:08 PM

Try checking the result of $this->getProduct()->getCategory().

like

<?php echo ($this->getProduct()->getCategory() ? $this->getProduct()->getCategory()->getName() : 'no category'?>

The problem is that catalog/product/view tries to set the category on the product model if it can figure it out.  First it looks for a URL parameter to load.  Absent that, it tries to find the last view category.  If the product can possibly be in that category, it uses that.  Otherwise it doesn’t set a category, which is the cause of your fatal error.  Products can exist in multiple categories, so it doesn’t know which one to show if you do not tell it.

Thanks Lee. Solution works great!

 
Magento Community Magento Community
Magento Community
Magento Community
 
brandeer
Sr. Member
 
Avatar
Total Posts:  133
Joined:  2009-03-18
switzerland
 

<!--category links on product view-->
                
<?php $count =0?>
                <?php $categories 
$_product->getCategoryIds(); ?>
                <?php 
foreach($categories as $k => $_category_id): ?>
                <?php $_category 
Mage::getModel('catalog/category')->load($_category_id?>
                
<a href="<?php echo $_category->getUrl() ?>" title="<?php echo $_category->getName() ?>">More <?php echo $_category->getName() ?></a>                
                
<?php $count++; 
                 if(
$count== 1) break; ?>
                <?php 
endforeach; ?>
                
<!-- End product category name  -->

for those who have items with more then one category and wants to show just one link

 
Magento Community Magento Community
Magento Community
Magento Community
 
Gustaaf
Jr. Member
 
Total Posts:  3
Joined:  2011-05-29
 

Thx for the code above. It works just fine in the product view.
Does anybody know how I can get the categories to also show on my cart succes page and the my orders page in the account?
I have tried this code, with no succes.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Kaushik Patel
Jr. Member
 
Total Posts:  28
Joined:  2011-12-24
 

try this its working…
http://hkpatel201.blogspot.in/2013/01/get-current-category-name-on-product.html

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top