【问题标题】:Show all subcategories of current parent caregory in sidebar navigation in MAGENTO在 MAGENTO 的侧边栏导航中显示当前父类别的所有子类别
【发布时间】:2014-02-25 20:05:04
【问题描述】:

尝试在 Magento 中构建侧边栏类别结构,以便在单击时显示活动类别的所有子项。使用下面的示例,当您进入主目录时,只会出现 Main Cats。然后,当单击任何 Sub Cat 时,会出现相应类别的子项,依此类推。

例如

Main Cat 1
    Sub Cat 1
        Sub/Sub 1
        Sub/Sub 1
        Sub/Sub 1
    Sub Cat 1
    Sub Cat 1
Main Cat 2
Main Cat 3

这是我当前的代码,但是一旦您进入最后一个类别,就只会显示 Main Cats(换句话说,如果您单击 Sub/Sub,菜单会关闭并仅显示 Main Cats)。

    <aside id="sidebar">
        <div class="sidebar-nav">
            <h2><?php echo $this->__('Products') ?></h2>
<ul>
<?php foreach ($store_cats as $cat) {
    if ($cat->getName() == $current_cat) {
        echo '<li><a href="'.$this->getCategoryUrl($cat).'">'.$cat->getName()."</a><ul>";
        foreach ($obj->getCurrentChildCategories() as $subcat) {
            echo '<li><a href="'.$this->getCategoryUrl($subcat).'">'.$subcat->getName()."</a></li>";
        }
        echo "</ul></li>";
    } else {
        echo '<li><a href="'.$this->getCategoryUrl($cat).'">'.$cat->getName()."</a></li>";
    }
} ?>
</ul>
        </div>
        <div class="sidebar-nav">
             <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('holiday-nav-links')->toHtml() ?> 
        </div>
        <div class="sidebar-nav">
             <?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('about-us-nav-links')->toHtml() ?> 
        </div>
    </aside>

非常感谢任何帮助。提前感谢您的帮助!

【问题讨论】:

    标签: magento navigation categories sidebar


    【解决方案1】:
    <?php $_helper = Mage::helper('catalog/category') ?>
    <?php $_categories = $_helper->getStoreCategories() ?>
    <?php $currentCategory = Mage::registry('current_category') ?>
    <?php if (count($_categories) > 0): ?>
        <ul>
            <?php foreach($_categories as $_category): ?>
                <li>
                    <a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
                        <?php echo $_category->getName() ?>
                    </a>
                    <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
                    <?php $_subcategories = $_category->getChildrenCategories() ?>
                    <?php if (count($_subcategories) > 0): ?>
                        <ul>
                            <?php foreach($_subcategories as $_subcategory): ?>
                                <li>
                                    <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
                                        <?php echo $_subcategory->getName() ?>
                                    </a>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    <?php endif; ?>
                </li>
            <?php endforeach; ?>
        </ul>
    <?php endif; ?>
    

    请查看此链接http://fishpig.co.uk/magento/tutorials/display-categories-and-subcategories-in-magento/

    希望对你有帮助

    【讨论】:

      【解决方案2】:

      几天前不得不做这件事。获取我的全部功能,但可能包含一些不必要的 html 部分。显示 2 级以上的类别(也适用于 3 级类别视图)

      public function getCatTree()
       {
              $treeHtml = '';
              $_helper = Mage::helper('catalog/category');
              $_categories = $_helper->getStoreCategories();
              $category = Mage::registry('current_category');
      
      
              $level = $category->getLevel();
              switch($level)
              {
                  case 4 :  
                          $level3Cat = $category->getParentCategory();
                          $level2Cat = $level3Cat->getParentCategory();
                          break;
      
                  case 3 :
                          $level2Cat = $category->getParentCategory();
                          break;
      
                  case 2 :
                          $level2Cat = $category;
                          break;
      
                  default :
                          $level2Cat = null;  
                          break;
              }
      
              //get the level 2 category ID
              $categoryId = $level2Cat->getId();
      
              if (count($_categories) > 0) 
              {           
                  foreach ($_categories as $_category) 
                  {
                      //match with the level 2 category, then list all its children
                      if ($_category->getId() == $categoryId) 
                      {
                          $_category = Mage::getModel('catalog/category')->load($_category->getId());
                          $_catChildrens = $_category->getAllChildren();
      
                          foreach(explode(',',$_catChildrens) as $index=>$child)
                          {
                                  $cat = Mage::getModel('catalog/category')->load($child);
      if($cat->getLevel() == 3 && $cat->getIsActive())
                                  {
                                      $isParent = ($cat->hasChildren()) ? 'parent' : '';
                                      $treeHtml .= '<li class="level1 nav'.$cat->getLevel().' '.$isParent.'">'.
                                                  '<a href="'.$cat->getUrl().'">'.
                                                    $cat->getName().
                                                      '</a>';
      
                                      if($cat->hasChildren())
                                      {
                                          $treeHtml .= '<div class="sub-menu-wrap"><ul class="level1">';
                                          foreach($cat->getChildrenCategories() as $indx=>$_subcategory)
                                          {
                                              if($_subcategory->getIsActive()) 
                                              {
                                                  $cat4 = Mage::getModel('catalog/category')->load($indx);
                                                  $treeHtml .= '<li class="level2 nav'.$cat4->getLevel().'">'.
                                                  '<a href="'.$cat4->getUrl().'">'.
                                                    $cat4->getName().
                                                      '</a></li>';
      
                                              }
                                          }
                                          $treeHtml .= '</ul></div>';
                                      }
                                      $treeHtml .= '</li>';   
                                  }
      
      
                          }
                          return $treeHtml;
       }
                  }
              }
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多