Call-back icon  Sales: Call 877.832.5289 (N America)|310.295.4144 (International)

Magento

eCommerce Software for Online Growth

Magento Forum

   
Subcategories for a parent category
 
Steve P. Sharpe
Jr. Member
 
Total Posts:  11
Joined:  2008-06-01
 

Hi all,

I need to be able to pull of a list of subcategories for a specified parent category id. For example I have two main categories setup (Departments & Flavours) on every page I want to show all of the subcategories for these main categories.

I see there is a getCurrentChildCategories function, which is kind of what I need but like this getChildCategories(4) - 4 being the parent id.

Hopefully that makes sense and what I’m asking is quite simple? I’ve searched the forum for a while and can’t find an answer.

Thanks in advance
Steve P. Sharpe

 
Magento Community Magento Community
Magento Community
Magento Community
 
Anders Rasmussen
Sr. Member
 
Avatar
Total Posts:  165
Joined:  2007-10-16
Denmark
 

I guess you can just copy the function, rename it to getChildCategories($parentId), and replace $category = $layer->getCurrentCategory(); with $category = Mage::getModel(’catalog/category’)->load($parentId);

 Signature 

Anders Rasmussen
Crius (in Danish)

 
Magento Community Magento Community
Magento Community
Magento Community
 
Steve P. Sharpe
Jr. Member
 
Total Posts:  11
Joined:  2008-06-01
 

Hi there,

I tried that, but got an error. However it did point me in the right direction. So thank-you!

For those that are interested this is what I did which is based on the getStoreCategories function.

left.phtml - 6 is the id of the parent category

<ul>
<?php foreach ($this->getSubCategories(6) as $_category): ?>
    <?php 
echo $this->drawItem($_category?>
<?php 
endforeach ?>
</ul>

/Mage/Catalog/Model/Category.php

public function getSubCategories($parentId)
    
{
        $helper 
Mage::helper('catalog/category');
        return 
$helper->getSubCategories($parentId);
    
}

/Mage/Catalog/Helper/Category.php

public function getSubCategories($parentId$sorted=false$asCollection=false$toLoad=true)
    
{
        $category 
Mage::getModel('catalog/category');
        
/* @var $category Mage_Catalog_Model_Category */
        
if (!$category->checkId($parentId)) {
            
if ($asCollection{
                
return new Varien_Data_Collection();
            
}
            
return array();
        
}

        $tree 
$category->getTreeModel();
        
/* @var $tree Mage_Catalog_Model_Resource_Eav_Mysql4_Category_Tree */

        
$nodes $tree->loadNode($parentId)
            ->
loadChildren()
            ->
getChildren();

        
$tree->addCollectionData(null$sorted$parentId$toLoadtrue);

        if (
$asCollection{
            
return $tree->getCollection();
        
else {
            
return $nodes;
        
}
    }

Hopefully this helps someone?

 
Magento Community Magento Community
Magento Community
Magento Community
 
ayayaaa
Jr. Member
 
Total Posts:  5
Joined:  2008-06-27
 

Hi,

I put this function in navigation.php and it works.

public function getSubCategories($parentId)
    
{
    
        $helper 
Mage::helper('catalog/category');
        return 
$helper->getSubCategories($parentId);
    
}

Thanks for your code

 
Magento Community Magento Community
Magento Community
Magento Community
 
toadx
Jr. Member
 
Total Posts:  20
Joined:  2008-06-19
 

It helped me a lot.  Thank you greatly my friend.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Hengky Irawan
Jr. Member
 
Avatar
Total Posts:  5
Joined:  2008-05-25
Indonesia
 

Hi,

Thank you for your code. It works like charm.
But, one question, how do I get the current parent category ID ?

Calviin

 Signature 

there’s no such thing as a coincidence. everything happens for a reason

 
Magento Community Magento Community
Magento Community
Magento Community
 
pdxwebhead
Jr. Member
 
Total Posts:  11
Joined:  2008-07-16
Portland Oregon
 

I’m trying to figure out how to apply this code to a left-nav example.. Where do I put put these code snippets to have a vertical nav display the child categories, or child products directly underneath the parent category when parent is selected ?

 Signature 

Learning Magento one mistake at a time....

 
Magento Community Magento Community
Magento Community
Magento Community
 
brad_frost
Jr. Member
 
Total Posts:  14
Joined:  2008-06-27
 

Hi, I am trying to implement Steve’s code and it is returning a PHP error:

WarningInvalid argument supplied for foreach()  in .../httpdocs/app/design/frontend/kirna_zabete/clothes_subcats/template/catalog/navigation/left.phtml on line 35

Here’s what I have implemented for left.phtml

<ul>
<?php foreach ($this->getSubCategories(3) as $_category): ?>
    <?php 
echo $this->drawItem($_category?>
<?php 
endforeach ?>
</ul>

Is there any specific place in each of the Category.php files where I need to place the respective functions? Can anyone offer some advice?

Thanks very much

 
Magento Community Magento Community
Magento Community
Magento Community
 
dcorrell
Jr. Member
 
Total Posts:  10
Joined:  2008-07-28
 

i get the same error as brad, any suggestions?

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
    Back to top
 
Sales: Call 877.832.5289 (North America) 310.295.4144 (International)
© Copyright 2008 Varien. Magento, eCommerce software, is a trademark of Irubin Consulting Inc. DBA Varien
Privacy Policy|Terms of Service
Magento Community Count
50099 users|439 users currently online|102231 forum posts