Getting and Using Categories and Sub Categories . .

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

This is an old revision of the document!


The first heading will show up as page title.

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.

[code] /* Get the categories that are active for the store */ $_main_categories=$this→getStoreCategories();

/* Get the current category the user is in */ $_current_category=$this→getCurrentCategory();

?> <ul> <?php

  /* This bit cycles through the categories - setting the next one to current */
  foreach ($_main_categories as $_main_category):
    if($_main_category->getIsActive()):                             
         $cur_category=Mage::getModel('catalog/category')->load($_main_category->getId());
         $layer = Mage::getSingleton('catalog/layer');
         $layer->setCurrentCategory($cur_category);     

/* Write the main categories */ ?> <li><a href=”<?php echo $this→getCurrentCategory()→getUrl()?>”><?php echo $this→getCurrentCategory()→getName();?></a></li>

<?php

/* Check the category variable loop against the current category path if it is - print sub categories */ if (in_array($this→getCurrentCategory()→getId(), $_categorypath)): ?> <?php $_maincategorylisting=$this→getCurrentCategory()?> <?php $_categories=$this→getCurrentChildCategories()?> <?php if($_categories→count()):?>

<ul> <? foreach ($_categories as $_category):?>

 <? if($_category->getIsActive()):                    
         $cur_subcategory=Mage::getModel('catalog/category')->load($_category->getId());
         $layer = Mage::getSingleton('catalog/layer');
         $layer->setCurrentCategory($cur_subcategory);  
        

?> <li><a href=”<?php echo $this→getCategoryUrl($_category)?>”> <?php echo $_category→getName()?></a></li>

  <? endif;?>

<?endforeach?> </ul> <?php /* This resets the category back to the original pages category If this is not done, subsequent calls on the same page will use the last category in the foreach loop */ ?> <?php $layer→setCurrentCategory($_maincategorylisting); ?> <?endif;?> <?endif;?>

                                                            

<?php endif; endforeach; ?>




 

Magento 2 GitHub Repository

Magento Job Board - Some sort of tag line goes here

Latest Posts| View all Jobs