|
Had a request for this code through email - sharing it with the group . .
Here is how I implemented a subcategory page that shows images and links to sub categories after the users clicks on a main category.
First off , in the catalog.xml file - I put the following reference in the “layers” section - so it’ll look like this:
<catalog_category_layered> <reference name="root"> <action method="setTemplate"><template>page/SubPage_2Column.phtml</template></action> </reference>
<reference name="subpage_left"> <block type="catalog/navigation" name="category.subnav" template="catalog/navigation/subcats.phtml" /> </reference>
</catalog_category_layered>
You’ll have to change your set template value (if you are changing the template) and also the reference name. In my template, subpage_left is the main content area.
Then, I created a subcats.phtml file and put it in the directory
/template/catalog/navigation subdirectory under my theme - you can put this in the default theme pack if you wish.
Here’s the code for subcats.phtml . . .
Some notes - the first little bit up at the top just prints out the current category name and description before printing out the subcategories.
in pseudo code here is what it does:
Print out the module header (Current Category Name and Description)
Get the Current Category and Save For Later
Get the Current Child Categories
For each child category
- Set the current category to be the child category
-Get the child category link, image, and name
-Print out child category link,image and name
Go to next child category until done
Reset the Current Category to the original current category - saved at top
If no child categories - display No matching products message
done
<h2><?php if($_currentCatName=$this->htmlEscape($this->getCurrentCategory()->getName())): ?><?php echo $_currentCatName ?></h2> <?php if($_description=$this->getCurrentCategory()->getDescription()): ?>
<p><?php echo $_description ?> </p> <?php endif; ?> <?php endif; ?>
<div id="categories"> <div class="col_full">
<h2>Product Categories</h2> <div class="catlisting"> <?php $_maincategorylisting=$this->getCurrentCategory()?> <?php $_categories=$this->getCurrentChildCategories()?> <?php if($_categories->count()):?> <? foreach ($_categories as $_category):?> <? if($_category->getIsActive()): $cur_category=Mage::getModel('catalog/category')->load($_category->getId()); $layer = Mage::getSingleton('catalog/layer'); $layer->setCurrentCategory($cur_category); if($_imageUrl=$this->getCurrentCategory()->getImageUrl()):?> <div><div class="linkimage"><p> <a href="<?php echo $this->getCategoryUrl($_category)?>"><img src="<?php echo $_imageUrl?>"></a></p></div><p><a href="<?php echo $this->getCategoryUrl($_category)?>"><?php echo $_category->getName()?></a></p></div> <? else: ?> <div><p><a href="<?php echo $this->getCategoryUrl($_category)?>"><?php echo $_category->getName()?></a></p></div>
<? endif; endif;?> <?endforeach?> <?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); ?>
</div> </div> </div>
<?php else: ?>
</div> <div class="note-msg"> <?php echo $this->__('There are no products matching the selection.') ?> </div>
</div> </div> <?endif;?>
|