0

I have a parent category (ID 10). I'd like to call the children of category ID 10's child categories. As per example below, is there any way to do this?

Parent Category > First Level Child > I want this one

Current code

$parentCategoryId = 141;//put your parent cat id here
$category = Mage::getModel('catalog/category')->load($parentCategoryId );
$children = Mage::getModel('catalog/category')->getCollection()->setStoreId(Mage::app()->getStore()->getId());
1

First of all, what you are trying to achieve is only possible if the Parent Category has only one child and that child is the "First Level Child" you want, unless you have the name which filters the one you want. There are two ways to pull this off.

1) Easiest one but only possible if you have the ID of the "First Level Child"

$parentId = 00; //Replace with the id of first level child
$children = Mage::getModel('catalog/category')->load($parentId)->getChildrenCategories();
foreach($children as $child){
    echo $child->getName();
}

2) This is exactly what you want but the category with ID 10 cannot have more than 1 children that are not in the same context otherwise it will return you all the other cats.

$parentId = 10;
$children = Mage::getModel('catalog/category')->load($parentId)->getChildrenCategories();
foreach($children as $child){
    $grandChildren = Mage::getModel('catalog/category')->load($child->getId())->getChildrenCategories();
    foreach($grandChildren as $grandChild){
        echo $grandChild->getName();
    }
}

Let me know if this worked for you.

  • Thanks for response Paul. The reason I ask is I am trying to do a brand listing page using the child categories. The parent category is brand - within that is A,B,C,D,E etc. and then beneath these the brand names. Sounds like it's not possible to just extract the brand name categories (i.e. grandchild)? – YorkieMagento Jun 30 '16 at 16:49
  • @YorkieMagento In this specific case of yours, the second solution I wrote in my answer will work perfectly for you, it will extract and list every brand name you have under the "Brands" category. Try it out and let me know! – user41363 Jun 30 '16 at 16:51
  • @YorkieMagento If this solved your issue please mark it as the correct answer! Thank you. – user41363 Jun 30 '16 at 17:05
  • Hi Paul, it doesn't return anything for me - only a blank page...I have changed the id to that of my parent category. – YorkieMagento Jun 30 '16 at 17:22
  • @YorkieMagento what is your Magento version? – user41363 Jul 1 '16 at 8:43

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.