1

Consider the following category tree:

A
  A1
    A11
  A2
    A21
    A22
B
  B1
C

By default, the categories listed in the layered navigation are the child categories of the current category, so for example if I opened up the A/A2 category in the front end, the following categories would be shown in the layered navigation:

A21
A22

Is there a way to always show the full category tree, starting from the root category? A lot of shops seem to do this but I haven't found a way yet to do this with Magento.

1

Just add this code on top of template/catalog/layer/view.phtml

$obj = new Mage_Catalog_Block_Navigation();
$store_cats = $obj->getStoreCategories();
$current_cat    = $obj->getCurrentCategory();
$current_cat_path    = $obj->getCurrentCategoryPath();
$current_cat_name   = (is_object($current_cat) ? $current_cat->getName() : '');

foreach ($store_cats as $cat) {
    if ($cat->getId() == $current_cat_path[0] || $cat->getId() == $current_cat_path[1]) {
        echo '<li class="current"><a href="../'.$cat->getData('request_path').'">'.$cat->getName()."</a>\n<ul>\n";
        foreach ($cat->getChildren() as $subcat) {
            if ($subcat->getId() == $current_cat_path[0]){
                echo '<li class="here"><a href="../'.$subcat->getData('request_path').'">'.$subcat->getName()."</a></li>\n";
            }else{
                echo '<li><a href="../'.$subcat->getData('request_path').'">'.$subcat->getName()."</a></li>\n";
            }
        }
        echo "</ul>\n</li>\n";
    } else {
        echo '<li><a href="../'.$cat->getData('request_path').'">'.$cat->getName()."</a></li>\n";
    }
}
| improve this answer | |
0

Add This in Block file or helper file (i prefer helper)

    public function getTreeCategories($parentId, $isChild)
{
    $allCats = Mage::getModel('catalog/category')->getCollection()
        ->addAttributeToSelect('*')
        ->addAttributeToFilter('is_active','1')
        ->addAttributeToFilter('include_in_menu','1')
        ->addAttributeToFilter('parent_id',array('eq' => $parentId))
        ->addAttributeToSort('position', 'asc');

    $class = ($isChild) ? "sub-cat-list" : "cat-list";
    $html .= '<ul class="'.$class.'">';

    foreach($allCats as $category) {

        $subcats = $category->getChildren();
        $subhtml = '';
        if(!empty($subcats)){
            $subhtml = $this->getTreeCategories($category->getId(), true);
        }


        $html .= '<li><div class="product_categories_list">';
                      if( (!empty($subhtml))){
                          $html .= '<span class="accor"></span>';
                      }else{
                          $html .= '<span class="no-accor"></span>';
                      }

                      if (count($_testproductCollection) <= 0)
                      {
                          $html.='<span class="product_block_count_zero">';
                          $html.= $category->getName();
                          $html.='</span>';
                      }
                      else{
                      $html .= '<a data-url="'.$category->getUrl().'"
                           name="'.cat.'" class="ajaxfilter"  value="'.$category->getId().'"
                            href="'.$category->getUrl().'">'.$category->getName().'</a>';
                      }

                    if(!empty($subhtml)){
                        $html .= $subhtml;
                    }

        $html .= '</div></li>';
    }
    $html .= '</ul>';
    return $html;
}

Now in the phtml file u can send call like this

$catlistHtml = $helper->getTreeCategories($ParentCatagoryID, false);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.