Magento - get parent category and all subcategories

I have one category that has 2 subcategories. There are 5 subcategories in each of these categories.

Is there a way to get a list of all these 10 sub-categories?

thanks

EDIT:

Something like that:

Main Category Sub_Cat_1 Cat_1 Cat_2 Cat_3 Sub_Cat_2 Cat_4 Cat_5 Cat_6 Wanting output like: Cat_1 Cat_2 Cat_3 Cat_4 Cat_5 Cat_6 

thanks

+4
source share
5 answers

It revealed:

 $cat = Mage::getModel('catalog/category')->load(24); $subcats = $cat->getChildren(); foreach(explode(',',$subcats) as $subCatid) { $_category = Mage::getModel('catalog/category')->load($subCatid); if($_category->getIsActive()) { $sub_cat = Mage::getModel('catalog/category')->load($_category->getId()); $sub_subcats = $sub_cat->getChildren(); foreach(explode(',',$sub_subcats) as $sub_subCatid) { $_sub_category = Mage::getModel('catalog/category')->load($sub_subCatid); if($_sub_category->getIsActive()) { echo '<li class="sub_cat"><a href="'.$_sub_category->getURL().'" title="View the products for the "'.$_sub_category->getName().'" category">'.$_sub_category->getName().'</a></li>'; } } } } 

Thanks for watching!

+5
source

All answers still load child categories in a loop, which, as a rule, is bad practice and causes the execution of many SQL queries, where one is enough.

Solution for a single request to execute:

Let $parentCategory be your main category, then this collection will load all the subcategories, at two levels below:

 $subcategoryCollection = Mage::getModel('catalog/category') ->getCollection() ->addFieldToFilter('level', $parentCategory->getLevel() + 2) ->addFieldToFilter('path', ['like' => $parentCategory->getData('path') . '/%']); 

The path field contains the category identifier with prefixes of all ancestor identifiers in the form 1/2/3 . A database is a column in catalog_category_entity that has an index, so a comparison like this has no performance issues.

+14
source

I developed a recursive function to get all children of the category

 $categoryObject = Mage::getModel('catalog/category')->load(CATID); $children = MODEL_OBJECT->getChildCategories($categoryObject); // IN MODLE FILE public $_catIds = array(); public function getChildCategories($categoryObject){ $categories = $categoryObject->getChildrenCategories(); foreach ($categories as $catgory){ if($catgory->hasChildren()){ $this->getChildCategories($catgory); } $this->_catIds[] = $catgory->getId(); } return $this->_catIds; } 

Hope this helps you.

+3
source
 $this->getCurrentCategory()->getParentCategory()->getData(); 

try it

-1
source

What I am doing is recursively finding the ancestor category identifier. Having found it, I can print the current categories of subcategories. If there are no children in the current category, I’ll just print a subcategory of my father. That way you can have infinite subcategories and still be able to display a list of subcategories.

  $_helper = Mage::helper("catalog/category"); $rootCat = Mage::app()->getStore()->getRootCategoryId(); $current = Mage::registry('current_category'); show_cat($current,$_helper,$rootCat); function show_cat($child,$_helper,$rootCat){ if ($child){ //look for anchestor $parentid = $child->getParentId(); $parent = Mage::getModel("catalog/category")->load($parentid); if($parentid != $rootCat) { //find the anchestor show_cat($parent,$_helper,$rootCat); }else{ //is root $_subcategories = $parent->getChildrenCategories(); if(count($_subcategories)>0){ echo '<ul>'; foreach($_subcategories as $_category){ echo '<li>'; echo '<a href="'.$_helper->getCategoryUrl($_category).'">'.$_category->getName().'</a>'; if($child->getId() == $_category->getId()){ $current = Mage::registry('current_category'); if ($current){ //handle current $_current_subcategories = $current->getChildrenCategories(); if(count($_current_subcategories)>0){ //the current cat has childrens echo '<ul>'; foreach($_current_subcategories as $_sub_category){ echo '<li>'; echo '<a href="'.$_helper->getCategoryUrl($_sub_category).'">'.$_sub_category->getName().'</a>'; echo '</li>'; } echo '</ul>'; }else{ //the current cat has no childrens $current_parent = $current->getParentId(); $current_parent = Mage::getModel("catalog/category")->load($current_parent ); $_current_subcategories = $current_parent ->getChildrenCategories(); echo '<ul>'; foreach($_current_subcategories as $_sub_category){ echo '<li>'; echo '<a href="'.$_helper->getCategoryUrl($_sub_category).'">'.$_sub_category->getName().'</a>'; echo '</li>'; } echo '</ul>'; } } } echo '</li>'; } echo '</ul>'; } } } } 
-1
source

Source: https://habr.com/ru/post/1346893/


All Articles