14

How can I get the top level parent category (just after Default Category) from current category object? I want the Category Name and Category URL of that parent category to show in the subcategories.

  • Default Category
    • Cat A
      • Cat B
        • Cat C

How can I get Cat A's Name and URL in Cat B and Cat C objects?

22

$category->getPath() will return the ids of all categories from the tree root to the current one separated by slash (/). Here is an example: 1/2/56/124/543. The first one is the 'root of roots'. The second one is the catalog root (default category). The rest of them are simple categories. So you can do something like this.

$path = $category->getPath();
$ids = explode('/', $path);
if (isset($ids[2])){
    $topParent = Mage::getModel('catalog/category')->setStoreId(Mage::app()->getStore()->getId())->load($ids[2]);
}
else{
    $topParent = null;//it means you are in one catalog root.
}

Now you can get the name and url like this:

if ($topParent){
    $name = $topParent->getName();
    $url = $topParent->getUrl();
}
7
  • Thanks Marius! There was a typo in getId() I corrected it. It's working perfectly.
    – MagExt
    Jun 10 '13 at 18:46
  • cool. sorry about the typo. and thanks for the correction.
    – Marius
    Jun 10 '13 at 18:47
  • Hi, can anyone refer me to the official magento document for the category tree?
    – MengT
    Oct 27 '14 at 14:20
  • @Marius when I get path of category all of category says 1/2/<Respective Category ID> My default category id is 2. Then what is this 1?
    – Butterfly
    Mar 8 '16 at 7:09
  • 1
    @invissible you are right.
    – Marius
    Mar 8 '16 at 7:22
15

Try this

$level = $this->getCurrentCategory()->getParentCategory()->getLevel();
if($level > 1){                 
    echo $this->getCurrentCategory()->getParentCategory()->getName();
    }
    else{
    echo $this->escapeHtml($_category->getName());
    }

Simply copy and paste this code :)

4
  • Hi. Thanks for your answer, but you're 5 months late :)
    – MagExt
    Nov 19 '13 at 11:17
  • 9
    Welcome to the Magento Stack. You are quite right to answer an old question if you have a good answer that hasn't already been covered. Answers are for the good of the community and not just for the benefit of the OP. Keep it up! :)
    – McNab
    Nov 20 '13 at 11:58
  • This answer will be wrong if you're on level 3 and deeper, as it returns first Parent, while question was about top one.
    – gnysek
    Mar 14 '17 at 16:04
  • While this isn't a complete solution, one could loop until the desired level is reached. Pushed me in the right direction.
    – Domino
    Apr 7 '17 at 14:21
4

You could use getParentCategories() on the current category and then call array_pop to get the last element. Do it twice to get the second last element.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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