Magento - get a parent category and all sub-sub-categories

后端 未结 5 908
无人共我
无人共我 2020-12-11 08:39

I have a single category, that has 2 subcategories. Within each of these categories, are 5 subcategories.

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

相关标签:
5条回答
  • 2020-12-11 09:20

    I have developed a recursive function to get all children of a 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 will help you.

    0 讨论(0)
  • 2020-12-11 09:22

    What I do is recursively find the id of the ancestor category. Once found it, I can print the current category sub categories. If the current category has no children, than I will just print the father's subcategory. In this way you can have infinite subcategories and still being able to display a sub category list.

                 $_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>';
                                }
                            }
                    }
                }
    
    0 讨论(0)
  • 2020-12-11 09:28
    $this->getCurrentCategory()->getParentCategory()->getData();
    

    try this

    0 讨论(0)
  • 2020-12-11 09:39

    All answers so far load children categories in a loop which is generally bad practice and causes execution of many SQL queries where a single one would suffice.

    Performant Single Query Solution:

    Let $parentCategory be your Main Category, then this collection will load all subcategories, 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 id prefixed with all ancestor ids in the form 1/2/3. Database wise it is a column in catalog_category_entity that has an index, so comparison like this has no performance issues.

    0 讨论(0)
  • 2020-12-11 09:39

    Figured it out:

    $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 looking!

    0 讨论(0)
提交回复
热议问题