Getting and Using Categories and Sub Categories . .

Last modified by Deathdragon on Fri, April 22, 2011 06:35
Source|Old Revisions  

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. /* Get the current category path */
  9. $_categorypath = $this->getCurrentCategoryPath();
  10. ?>
  11. <ul>
  12. <?php
  13. if ($_main_categories):
  14.     /* This bit cycles through the categories - setting the next one to current */
  15.     foreach ($_main_categories as $_main_category):
  16.       if($_main_category->getIsActive()):                             
  17.            $cur_category=Mage::getModel('catalog/category')->load($_main_category->getId());
  18.            $layer = Mage::getSingleton('catalog/layer');
  19.            $layer->setCurrentCategory($cur_category);     
  20.  
  21. /* Write the main categories */           
  22. ?>               
  23. <li><a href="<?php echo $this->getCurrentCategory()->getUrl()?>"><?php echo $this->getCurrentCategory()->getName();?></a></li>   
  24.  
  25.  
  26. <?php
  27.  
  28. /* Check the category variable loop against the current category path if it is - print sub categories */
  29. if (in_array($this->getCurrentCategory()->getId(), $_categorypath)): ?>
  30. <?php $_maincategorylisting=$this->getCurrentCategory()?>                       
  31. <?php $_categories=$this->getCurrentChildCategories()?>
  32. <?php if($_categories->count()):?>
  33.  
  34. <ul>
  35. <? foreach ($_categories as $_category):?>                   
  36.    <? if($_category->getIsActive()):                   
  37.            $cur_subcategory=Mage::getModel('catalog/category')->load($_category->getId());
  38.            $layer = Mage::getSingleton('catalog/layer');
  39.            $layer->setCurrentCategory($cur_subcategory)
  40.          
  41. ?>                         
  42. <li><a href="<?php echo $this->getCategoryUrl($_category)?>"> <?php echo $_category->getName()?></a></li>
  43.     <? endif;?>
  44. <?endforeach?>
  45. </ul>           
  46. <?php /* This resets the category back to the original pages category
  47. ****     If this is not done, subsequent calls on the same page will use the last category
  48. ****    in the foreach loop
  49. */   ?>
  50.  
  51. <?endif;?>   
  52.  
  53. <?endif;?>
  54.                                  
  55. <?php $layer->setCurrentCategory($_current_category); ?> 
  56.                                                              
  57. <?php         
  58. endif;
  59. endforeach;
  60. else:
  61. ?>
  62. <p>$_main_categories array was empty.</p>
  63. <p>This might be because you are referencing this phtml file with a wrong type attribute. You should use <block type="catalog/navigation" ... /> !</p>
  64. <?php endif; ?>



 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs