0

I'm working on product page in which I've to display the current subcategory. I've searched a lot but all I could find is to display all the categories and subcategories. How would I get only a subcategory in which I'm i.e. current subcategory. I'm using following code-

    <?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) ?>">
                    <option><?php echo $_subcategory->getName() ?>
                    </option>
                  </a>
                 </li>
                 <?php endforeach; ?>
                </ul>
               <?php endif; ?>
               </li>
           <?php endforeach; ?>
         </ul>
     <?php endif; ?>

Please some one help me out.

1 Answer 1

0

You can get subcategories of current category with product count on product page.

<?php
$parentCatId = Mage::registry('current_category')->getId(); 
$root = Mage::getModel('catalog/category')->load($parentCatId);
$subCat = explode(',',$root->getChildren()); 

$collection  = $root
             ->getCollection()
             ->addAttributeToSelect("*")
             ->addFieldToFilter("entity_id", array("in", $subCat) );
?>
 <?php foreach($collection as $catname){
$qty = Mage::getModel('catalog/category')->load($catname->getId())->getProductCount();?>
  <li class="nav-item"><a href="<?php echo $catname->getUrl(); ?>"><?php echo $catname->getName(); ?>(<?php echo $qty; ?>)</a></li>
 <?php   }; ?> 

Hope, it will be helpful.

2
  • Thanks for your reply... but It's not working, In fact it's giving me an error "Fatal error: Call to a member function getId() on null in /home/retrntrip/public_html/magento/app/design/frontend/arisen/sneezeguard/template/catalogsearch/form.mini.phtml on line 83" Dec 21, 2016 at 13:19
  • Replace first line with below code : <?php $layer = Mage::getSingleton('catalog/layer'); $_category = $layer->getCurrentCategory(); $parentCatId= $_category->getId(); $root = Mage::getModel('catalog/category')->load($parentCatId); ?> it is working fine for me. Dec 22, 2016 at 5:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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