For example I have a product called Nike shoes and it has categories Catalog > Shoes > Men.

What I need is just to get the name of the last category (Men). At first glance it can be retrieved very easily:

$cat = $_product->getCategoryIds();
$name= Mage::getModel('catalog/category')->load( end($cat) )->getName();

But there is a big problem, function getCategoryIds() return array of category ID's sorted numerically - so if last category (e.g. Men) has smaller index then previous one (e.g. Shoes) - I'll get incorrect result (Shoes).

Question: how to get list of category ID's sorted in the same way as on "Manage Categories" page? Or just how to get the last category?

Thank you

  • The problem here is that you can have a product nested n-depth in any number of categories. Are you trying to get this as a function of maximum depth or the current category that the product is being viewed within?
    – philwinkle
    Commented May 31, 2013 at 21:04
  • I want to get the name of category of a product, but only the one that has highest n-depth. Thanks!
    – Marvin3
    Commented May 31, 2013 at 21:12

1 Answer 1


I hope someone comes up with a better solution, because I'm pretty sure mine if far from being perfect, sub-optimal, and VERY hacky. Anyway, when I need the most specific category related to a product, I do something like this:

$categoryIds = $product->getCategoryIds();
$collection = Mage::getModel('catalog/category')->getCollection()
        ->addAttributeToFilter('entity_id', $categoryIds)
        ->addAttributeToFilter('is_active', 1);
$collection->getSelect()->order('level DESC')->limit(1);

$name = $collection->getFirstItem()->getName();

By sorting the categories by their level, you will get the most specific one by calling getFirstItem(). Hopefully there's a better/easier way to do it which I'm not aware of, but this one works for sure.

I should note that, in case you have 2+ categories which are on the same level, you will get only one of them, specifically the first one which appears in the collection. The result is not so easy to predict, but it was usually good enough for what I needed to do.

  • According to the OP they need the maximum n-depth, perhaps this is good enough. You can also chain via fluent interface the addAttributes, and you can get the collection without specifically referring to getResourceModel via Mage::getModel('catalog/category')->getCollection()
    – philwinkle
    Commented May 31, 2013 at 21:55
  • I edited it, but it still doesn't look much prettier :-) Still, it gets the job done
    – fmrng
    Commented May 31, 2013 at 22:23
  • Thank you guys, it works, but adds a few dozens of queries to catalog page.
    – Marvin3
    Commented Jun 1, 2013 at 13:15
  • In what way does it add a few dozen queries?
    – philwinkle
    Commented Jun 1, 2013 at 14:49
  • @philwinkle I add this code to every product in catalog, and very usually all of them have mixed categories.
    – Marvin3
    Commented Jun 2, 2013 at 12:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.