Home » Magento

Magento: Get category name and url from product

7 December 2009 Share/Bookmark

$product->getCategoryIds() gives array of category ids which the product is associated to. We can loop through this array and load each category to get the category name and url.

<?php
/**
 * get categories from a product
 */
$categoryIds = $product->getCategoryIds();
/**
 * looping through the array of category ids
 */
foreach($categoryIds as $categoryId) {
	$category = Mage::getModel('catalog/category')->load($categoryId);
	?>
		Category: <a href="<?php echo $category->getCategoryUrl() ?>"><?php echo $category->getName() ?></a><br/>
	<?php
}
?>

From Mukesh Chapagain's Blog, post Magento: Get category name and url from product

email

php magento mukesh chapagain

Get New Post by Email
RSS Feed Subscribe RSS Feed
  • http://www.mehedi.com.bd Mehedi

    Thanks a lot my dear friend. :)

  • Johan Jacobs

    How do you make this work on the product search page?

    Category info seems to be only loaded on product pages and category pages …

  • Bhushan

    Hi
    I am getting following error.
    “Fatal error: Call to a member function getCategoryIds() on a non-object in ..”. Plesae help. I have product ID. I WANT TO FETCH RECENT 5 PRODUCTS OF THAT CATEGORY. HOW TO ACHIEVE THIS? Please guide me.

  • Bartosz Górski

    If getCategoryUrl() method doesn’t work for someone, try getUrlPath(). Works for magento 1.5.1

  • Bartosz Górski

    If getCategoryUrl() method doesn’t work for someone, try getUrlPath(). Works for magento 1.5.1

  • Anonymous

    Hi,  I tried your code, but it didn’t work.

    here the code I added app/code/core/Mage/GoogleAnalytics/Block/Ga.php

               $categoryIds = $item->getCategoryIds();           $categories = null;            foreach($categoryIds as $categoryId) {              $category = Mage::getModel(‘catalog/category’)->load($categoryId);              $categories[] = $category->getName();            }            $html .= ‘_gaq.push([“_addItem”,’;            $html .= ‘”‘ . $order->getIncrementId() . ‘”,’;            $html .= ‘”‘ . $this->jsQuoteEscape($item->getSku(), ‘”‘) . ‘”,’;            $html .= ‘”‘ . $this->jsQuoteEscape($item->getName(), ‘”‘) . ‘”,’;            $html .= ‘”‘ . implode(‘,’, $categories ) . ‘”,’;

    I’m using Magento: 1.4.1.0

  • Andrew Willshire

    Hi Bartosz, and Mukesh, thanks so much for your brilliant site!!!

    I am finding that the only way to get the URL in 1.5.1 is just using “getUrl”.  If you use “getUrlPath”, it only returns the portion of the path after your domain url, eg: just “category_name.html”.  If you use “getUrl”, you get the full path to the category.

    Cheers,
    Andrew.

  • Mike

    Works fine, I just had to use $_product instead of $product.

  • Bhushan Bamania

    Not works,think loop not initiated!!!!!!!

  • Bhushan Bamania

    Not works,think loop not initiated!!!!!!!

  • Bhushan Bamania

    Thanx buddy….it works fine..
    i had some mistake in my code..
    thanx again…

  • Om Patel49

    Thanks great job done by you

  • Spider

    Working, Nice work Mate.