Thursday, June 7, 2012

Displaying Subcategory or Top Level Subcategory in Left Nevegation in magento

Hi, Some time we need to display all Subcategory of the current category that is the level2 of current category’s subcategory so to solve this issue just set your current category id or just get current category id and set to $currentCategoryID variable i keep this file in root and run in browser you can make it use of this code and call in left navigation in list page



<?php

 define('MAGENTO', realpath(dirname(__FILE__)));
 require_once MAGENTO . '/../app/Mage.php';
 Mage::app();
 $currentCategoryID=99;
 
 function getParentTopCategory($category)
    {
        if($category->getLevel() == 2){
            return $category;
        } else {
            $parentCategory = Mage::getModel('catalog/category')->load($category->getParentId());
            return getParentTopCategory($parentCategory);
        }
    }
 function Subcategory($subcats){
 
  echo '<ul>'; foreach($subcats as $_category)
        {
           // $_category = Mage::getModel('catalog/category')->load($subCatId);
            if($_category->getIsActive())
            {
                $caturl = $_category->getURL();
                $catname = $_category->getName();
     echo '<li>';
                echo '<a href="'.$caturl.'" title="'.$catname.'">'.$catname.'</a>';
    echo '</li>';
     $subcats3 = $_category->getChildrenCategories();
      if ($subcats3) {
      Subcategory($subcats3);
      }
     
    
    
     
     
            }
        }
  echo '</ul>';
 
 }

  //  $openCatId =  getParentTopCategory(Mage::getModel('catalog/category')->load($currentCategoryID))->getId();
  //OR you can use below code to get parent catgeory id of lavel 2 
  
 $path=explode('/',Mage::getModel('catalog/category')->load($currentCategoryID)->getPath());
 $openCatId= $path[2];
    $cat = Mage::getModel('catalog/category')->load($openCatId);
    
    $subcats = $cat->getChildrenCategories();
    
    if ($subcats) {
        foreach($subcats as $_category)
        {
            if($_category->getIsActive())
            {
                $caturl = $_category->getURL();
                $catname = $_category->getName();
                echo '<a href="'.$caturl.'" title="'.$catname.'">'.$catname.'</a><br>';
     $subcats2 = $_category->getChildrenCategories();
    
    //  exit;
      if ($subcats2) {
      Subcategory($subcats2);
      }
            }
        }
        
    }
 ?>


http://www.magentocommerce.com/boards/viewthread/283368/

No comments:

Post a Comment