Magento Forum

   
Subcategory view
 
seldon
Sr. Member
 
Total Posts:  92
Joined:  2007-11-08
 

Sometimes, when you have a category tree structure, you do not just want to show the products that are in some category, but you also want to display the subcategories of that category together with their descriptions. Would it be possible to include such a view ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
[db] DigitalBlueprint
Member
 
Avatar
Total Posts:  37
Joined:  2007-10-11
Eastbourne, UK
 

I have implemented something similar to this I think, although only with images and category names rather than a description. You can see an example here.

Is that the type of thing you mean? If so, I’ll see if I can pull out the changes I made in order to achieve this.

 
Magento Community Magento Community
Magento Community
Magento Community
 
seldon
Sr. Member
 
Total Posts:  92
Joined:  2007-11-08
 

Thats exactly what i mean! Could you please share the code necessary to do that? (or explain to me the required modifications?) I would be very grateful! Thanks!

 
Magento Community Magento Community
Magento Community
Magento Community
 
[db] DigitalBlueprint
Member
 
Avatar
Total Posts:  37
Joined:  2007-10-11
Eastbourne, UK
 

OK, this was a little more complicated than it should have been as it would appear that for some reason category images are not available through the normal collection that is returned in the Navigation block.

So… first, create a new method to return full category information ready for display:

## app/code/core/Mage/Catalog/Block/Navigation.php

    public function getCurrentChildCategoriesForSubcatDisplay()
    
{
        $layer 
Mage::getSingleton('catalog/layer');
        
$category   $layer->getCurrentCategory();
        
$collection Mage::getResourceModel('catalog/category_collection')
            ->
addAttributeToSelect('name')
            ->
addAttributeToSelect('url_key')
            ->
addAttributeToSelect('image')
            ->
addIdFilter($category->getChildren())
            ->
load();
                    
        return 
$collection;
    
}

Next, update your templates main.xml file to pull in the subcategory template:

## app/design/frontend/default/(yourtheme)/layout/main.xml

<!--
Category default layout
-->

    <
catalog_category_default>
        <
reference name="left">
            <
block type="catalog/navigation" name="catalog.leftnav" before="-" template="catalog/navigation/left.phtml"/>
            <
block type="directory/currency" name="currency" before="-" template="directory/currency.phtml"/>
        </
reference>
        <
reference name="content">
            <
block type="catalog/navigation" name="category.subcategories" template="catalog/category/sub.categories.phtml"/>
            <
block type="catalog/category_view" name="category.products" template="catalog/category/view.phtml">
                <
block type="catalog/product_list" name="product_list"></block>
            </
block>
        </
reference>
    </
catalog_category_default>

<!--
Category layered navigation layout
-->

    <
catalog_category_layered>
        <
reference name="left">
            <
block type="catalog/layer_view" name="catalog.leftnav" before="-" template="catalog/layer/view.phtml"/>
            <
block type="directory/currency" name="currency" before="-" template="directory/currency.phtml"/>
        </
reference>
        <
reference name="content">
            <
block type="catalog/navigation" name="category.subcategories" template="catalog/category/sub.categories.phtml"/>
            <
block type="catalog/category_view" name="category.products" template="catalog/category/view.phtml">
                <
block type="catalog/product_list" name="product_list"></block>
            </
block>
        </
reference>
    </
catalog_category_layered>

Note, the only change here are the additional lines which reference the sub.categories.phtml template.

Now, just create your subcategory template file:

## app/design/frontend/default/(yourtheme)/template/catalog/category/sub.categories.phtml

<?$_categories=$this->getCurrentChildCategoriesForSubcatDisplay()?>
<?if
($_categories->count()):?>
<div class="box generic-box category-thumbs-container">
    <
h3>Sub Categories</h3>
    <
ul class="subcategory-thumbs">
        
<?foreach ($_categories as $_category):?>
        
<li>
            <
a href="<?=$_category->getCategoryUrl();?>">
                <
img src="<?=$_category->getImageUrl();?>" width="90" height="90" alt="<?=$_category->getName();?>" title="<?=$_category->getName();?>" />
                <
br />
                <
h3><?=$_category->getName();?></h3>
            </
a>
        </
li>
        
<?endforeach?>
    
</ul>
</
div>
<?endif;?>

Hope that helps.

This was done initially for the 0.6.13160 release, so it may possible to use the standard getCurrentChildCategories() function in 0.6.13700 rather than the modified one above.

 
Magento Community Magento Community
Magento Community
Magento Community
 
seldon
Sr. Member
 
Total Posts:  92
Joined:  2007-11-08
 

Exactly what i wanted! Thnx, ill let you know if it works!

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top