【问题标题】:category tree in magento with more level on the frontendMagento中的类别树在前端具有更多级别
【发布时间】:2012-08-21 15:21:15
【问题描述】:

谢谢........但是......我有下面的代码,它正在工作到第一级而不是类别树中的更多级别,有人可以帮助我达到第三级及更多......类别树的级别............这意味着如果我单击父类别,则只有该特定父级与他的孩子一起打开所有其他将关闭方式 像 类别1 -子类别1 ----子子类1 -subcategory2

类别 2 -子类别1 -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";
                       }
                 }
         ?>

【问题讨论】:

    标签: magento


    【解决方案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.}
    

    这样可以避免在触底时出现错误。

    【讨论】:

    • 它不工作我收到错误:----\ NetworkError: 500 Internal Server Error - localhost/magento/index.php/women-shoes.html"
    • 是否放入了 if 语句?如果是,则可能是我们需要用当前类别更新 $obj ($current_cat)(它可能会继续在父类别 var_dump 上循环以查看发生了什么)
    • 仍然是同样的问题..........你能为我的目录写我的left.phtml文件的完整代码吗:--frontend/default/default/template/catalog/ navigation/left.phtml......这样我就可以在magento前端页面的左侧获取类别树......感谢您的帮助............
    • 这里有个问题有你需要的代码stackoverflow.com/questions/5564647/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多