douyanlu7380 2014-05-07 11:30
浏览 23

如何获取magento中的所有3个级别的类别?

I want to show all top level categories and its respective 2 level of subcategories. for example Root category(id=3)

-Electronics
  --Computers
   ---Dell
   ---Samsung
   ---Accer

 -Games Movies and Musics
  --Games
   ---Pc Games
   ---Mobile Games

Here Electronics and Games Movies and Musics is top level categories I don't want to display root category, only from top level categories i.e from electronics and Games Movies and Musics

  • 写回答

1条回答 默认 最新

  • du2986 2014-05-07 11:37
    关注

    Like this

    $category_id = 'Electronics category id'
    $children = Mage::getModel('catalog/category')->getCategories($category_id);
    foreach ($children as $category) {
    echo $category->getName();
    }
    

    Update: You can keep looping through them like this

    $category_id = 'Electronics category id';
        $children = Mage::getModel('catalog/category')->getCategories($category_id);
        foreach ($children as $category) {
            echo '<h1>'.$category->getName().'</h1>';
            if($category->getChildren()){
                $childrensChildren = $category->getChildren();
                foreach($childrensChildren as $ChildCategory){
                    echo '<h2>'.$ChildCategory->getName().'</h2>';
                }
            }
    
        }
    
    评论

报告相同问题?