Getting and Using Categories and Sub Categories . .

Last modified by MuschPusch on Thu, June 24, 2010 16:50
Source|Old Revisions  

This is an old revision of the document!


I can’t believe I’m the first to document this on the wiki, but here it goes . .

The code below does a couple of things . . . First it will get all the store’s categories - it then checks to see if they are active before continuing.

Also - it might be helpful to have the sub categories displayed, right? Well, in my case, I only wanted the subcategory for the current category displayed - so I put a check in to do so.

  1. <?PHP
  2. /* Get the categories that are active for the store */
  3. $_main_categories=$this->getStoreCategories();
  4.  
  5. /* Get the current category the user is in */
  6. $_current_category=$this->getCurrentCategory();
  7.  
  8. ?>
  9. <ul>
  10. <?php
  11.     /* This bit cycles through the categories - setting the next one to current */
  12.     foreach ($_main_categories as $_main_category):
  13.       if($_main_category->getIsActive()):                             
  14.            $cur_category=Mage::getModel('catalog/category')->load($_main_category->getId());
  15.            $layer = Mage::getSingleton('catalog/layer');
  16.            $layer->setCurrentCategory($cur_category);     
  17.  
  18. /* Write the main categories */           
  19. ?>               
  20. <li><a href="<?php echo $this->getCurrentCategory()->getUrl()?>"><?php echo $this->getCurrentCategory()->getName();?></a></li>   
  21.  
  22.  
  23. <?php
  24.  
  25. /* Check the category variable loop against the current category path if it is - print sub categories */
  26. if (in_array($this->getCurrentCategory()->getId(), $_categorypath)): ?>
  27. <?php $_maincategorylisting=$this->getCurrentCategory()?>                       
  28. <?php $_categories=$this->getCurrentChildCategories()?>
  29. <?php if($_categories->count()):?>
  30.  
  31. <ul>
  32. <? foreach ($_categories as $_category):?>                   
  33.    <? if($_category->getIsActive()):                   
  34.            $cur_subcategory=Mage::getModel('catalog/category')->load($_category->getId());
  35.            $layer = Mage::getSingleton('catalog/layer');
  36.            $layer->setCurrentCategory($cur_subcategory)
  37.          
  38. ?>                         
  39. <li><a href="<?php echo $this->getCategoryUrl($_category)?>"> <?php echo $_category->getName()?></a></li>
  40.     <? endif;?>
  41. <?endforeach?>
  42. </ul>           
  43. <?php /* This resets the category back to the original pages category
  44. ****     If this is not done, subsequent calls on the same page will use the last category
  45. ****    in the foreach loop
  46. */   ?>
  47. <?php $layer->setCurrentCategory($_maincategorylisting)?>
  48. <?endif;?>   
  49.  
  50. <?endif;?>                                   
  51.                                                              
  52. <?php         
  53. endif;
  54. endforeach; ?>



 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs