magento类别搜索

时间:2012-09-17 07:17:51

标签: magento magento-1.6

我在类别树中导航下有两个主要子项,如下图所示

enter image description here

enter image description here

我想列出地区活动的类别和子类别,我尝试了两种不同的功能

$collection = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('name')->load();
    foreach ($collection as $cat) {
        echo $cat->getName();
    }  

这将返回Region和Actities中的所有孩子以及另一个函数

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

Reference link

这只返回子类别

我想列出所有类别,子类别和子子类别等等......对于Region

提前感谢

1 个答案:

答案 0 :(得分:1)

试试这个:

    $rootcatId= 10;
    $categories = Mage::getModel('catalog/category')->getCategories($rootcatId);
    function  get_categories($categories) {
        $array= '<ul>';
        foreach($categories as $category) {
            $cat = Mage::getModel('catalog/category')->load($category->getId());
            $count = $cat->getProductCount();
            $array .= '<li>'.
            '<a href="' . Mage::getUrl($cat->getUrlPath()). '">' .
                      $category->getName() . "(".$count.")</a>\n";
            if($category->hasChildren()) {
                $children = Mage::getModel('catalog/category')->getCategories($category->getId());
                 $array .=  get_categories($children);
                }
             $array .= '</li>';
        }
        return  $array . '</ul>';
    }
    echo  get_categories($categories);