magento中的类别树,前端有更多级别

时间:2012-08-21 15:21:16

标签: magento

谢谢..............但是......我的下面的代码正在升级到第1级而不是更高级别的类别树可以有人帮我3级以上...........类别树的等级...........这意味着如果我将点击父类别,只有特定的父母与他的孩子一起打开所有其他将关闭方式 喜欢 组别 -subcategory1 ---- subsubcategory1 -subcategory2

类别2 -subcategory1 -subcategory2

      <?php
          $obj = new Mage_Catalog_Block_Navigation();
          $store_cats   = $obj->getStoreCategories();
          $current_cat  = $obj->getCurrentCategory();
           $current_cat = (is_object($current_cat) ? $current_cat->getName() : '');

            foreach ($store_cats as $cat) {
                  if ($cat->getName() == $current_cat) {
                        echo '<li class="current"><a href="'.$this->getCategoryUrl($cat).'">'.$cat->getName()."</a>\n<ul>\n";
                        foreach ($obj->getCurrentChildCategories() as $subcat) {
                        echo '<li><a href="'.$this->getCategoryUrl($subcat).'">'.$subcat->getName()."</a></li>\n";
                  }
                   echo "</ul>\n</li>\n";
                  } else {
                       echo '<li><a href="'.$this->getCategoryUrl($cat).'">'.$cat->getName()."</a></li>\n";
                       }
                 }
         ?>

1 个答案:

答案 0 :(得分:1)

解决此问题的最简单方法是创建一个递归函数(一个自我调用的函数)。

以下是您可能需要设置代码的方法:

//go through all the parent catgeroies
foreach ($store_cats as $cat) {
        // if it's the category we are looking for let's spit it out as an <li>
        if ($cat->getName() == $current_cat) {
                    echo '<li class="current"><a href="'.$this->getCategoryUrl($cat).'">'.$cat->getName()."</a>\n<ul>\n";
                    // let's get all the subcategories no matter how deep (look at function below).
                    getChildCategories();

       }
}
//our new sub-category getter
public function getChildCategories() {

            // lets loop through all the children of the current category and spit out <li> for them    
            foreach ($obj->getCurrentChildCategories() as $subcat) {
                         echo '<li><a href="'.$this->getCategoryUrl($subcat).'">'.$subcat->getName()."</a></li>\n";

                        //lets call ourself again to see whether there are deeper layer to be found
                         getChildCategories();
              }
}

您想要添加到代码中的是if语句,检查是否有子代:

   if ($obj->getCurrentChildCategories()) {//then loop through etc.}

这样你就可以避免在达到最低点时遇到错误。