Home » Magento7 December 2009

Magento: Get all categories name and url of a product

This article shows how you can fetch all the categories related/associated with a particular product.

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

Instead of loading category inside the loop, we can fetch categories of a product using category collection model as well. This is a better way than the above one.

Hope this helps. Thanks.

Magento

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed
  • 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.

  • Jitender Malav

    i want to get product category through product url is it possible or not