Home » Magento

Magento: Get category name and url from product

7 December 2009 17,700 views Popularity: 36% Share/Bookmark

email

$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
}
?>

Related posts:

  1. Magento: How to filter product collection using 2 or more category filters?
  2. Magento 1.4: No products displayed in category listing
  3. Magento: Get sub categories and product count
  4. Magento: Get current and parent category
  5. Magento: Adding category attributes
  6. Magento: Get parent id of simple product associated to configurable product
  7. Magento: Sort latest product by ‘created date’ and ‘new from date’
  8. Magento: Get Product Collection by Type
  9. Magento: How to get all associated children product of a configurable product?
  10. Magento: Get Bestselling products by category and date time
  • 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.