Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

How to draw subcategories
 
snooocy
Jr. Member
 
Total Posts:  3
Joined:  2007-09-06
 

Hi,

I try to build a left navigation element with category links of 2nd level subcategories. I found a way to draw all subcategories from a category ID, but how can I get only the subcategories from the second level?

My Categories:
Main Category (ID=29)
Subcategory (level1)
Subcategory (level1)
Subcategory (level1)
--2. Category
--Subcategory (level2)
--Subcategory (level2)
--Subcategory (level2)
---3. Category
---Subcategory (level3)
---Subcategory (level3)
---Subcategory (level3)

What I need:
Subcategory (level2)
Subcategory (level2)
Subcategory (level2)

Here is the code to draw all subcategories from category ID 29:
my_own_left.phtml

<div class="box">
    <
div class="side-nav">
        <
h4 class="no-display"><?php echo $this->__('Category Navigation:'?></h4>
        <
ul id="side-nav" >
        
<?php foreach ($this->getStoreCategories() as $_category): ?>
        <?php 
if ($_category->getId() == '29'{
                
echo $this->drawItemAll($_category).$_category->getId();
              

              ?>
        <?php 
endforeach ?>
        
</ul>
    </
div>
    
<?php echo $this->getChildHtml('topLeftLinks'?>
</div>

Can anybody help me, please?

Thanks,
Andre

 
Magento Community Magento Community
Magento Community
Magento Community
 
alkarim
Guru
 
Total Posts:  368
Joined:  2008-04-10
 

hi there, May I look at your my_own_left.php , cause at my file.phtml “$this->drawItemAll” is not working

thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
snooocy
Jr. Member
 
Total Posts:  3
Joined:  2007-09-06
 

Hi Alkarim,

I added my own “drawItemAll” function to the navigation.php for some tests and forgot this point in my last post.

So here is the right code to draw all subcategories from categorie ID 29.

my_own_left.phtml:

<div class="box">
    <
div class="side-nav">
        <
h4 class="no-display"><?php echo $this->__('Category Navigation:'?></h4>
        <
ul id="side-nav" >
        
<?php foreach ($this->getStoreCategories() as $_category): ?>
        <?php 
if ($_category->getId() == '29'{
                
echo $this->drawItem($_category).$_category->getId();
              

              ?>
        <?php 
endforeach ?>
        
</ul>
    </
div>
    
<?php echo $this->getChildHtml('topLeftLinks'?>
</div>

Has anybody an idea how to draw only the subcatogories from the second category (level2)?

Thanks,
Andre

 
Magento Community Magento Community
Magento Community
Magento Community
 
alkarim
Guru
 
Total Posts:  368
Joined:  2008-04-10
 

hi @snooocy

try this at : app/code/core/Mage/Catalog/Block/Navigation.php

public function drawItemHw($category$id{
        
if ($category->getId() == $id) return $category->getName();
         
        
$children $category->getChildren();
        
$hasChildren $children && $children->count();
        if (
$hasChildren{
            
foreach ($children as $child$this->drawItemHw($child$id);
        
}
        
return '';
    
}
        
    
public function getDrawItemHw($id{
        $_categories 
$this->getStoreCategories();
        
$flag false;        

        foreach (
$_categories as $_category):
            
$sCategoryName $this->drawItemHw($_category$id);
            if (!empty(
$sCategoryName)) return $sCategoryName;
        endforeach;

        return 
'';
    
}

then at yours my_own_left.phtml :

you can call the category base the id from the product :

echo Mage::getBlockSingleton('catalog/navigation')->getDrawItemHw(29);

Hope this would help you

 
Magento Community Magento Community
Magento Community
Magento Community
 
snooocy
Jr. Member
 
Total Posts:  3
Joined:  2007-09-06
 

...thanks for your suggestion @alkarim! I tried your Navigation.php enhancement, but I only get back the name of cat ID 29(Main Category)! Nothing else, the subcategories are still missing. Any other ideas?

I think I need something like this in the Navigation.php

public function getSubcategories($category$id=29,$level=2{
     
...
     return 
$subcategories;
}

Result:
Subcategory (level2)
Subcategory (level2)
Subcategory (level2)

Andre

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