iamjonesy
iamjonesy

Reputation: 25122

Magento: Can't get categories children

For some reason this code is returning NULL when trying to get a categories subcategories.

 <?php var_dump($_category->getChildrenCategories()); ?>

Here is the full code from a .phmtl file.

<ul id="nav_vert">
            <?php foreach ($this->getStoreCategories() as $_category): ?>
            <?php if ($_category->getIsActive()) { ?>
            <?php $open = $this->isCategoryActive($_category); ?>
            <?php $potential = $_category->hasChildren(); ?>
            <li><a href="<?php echo $this->getCategoryUrl($_category); ?>"<?php if($open) { echo ' class="open"'; } ?><?php if($potential) { echo ' class="potential"'; } ?> ><?php if($potential&&$open) { echo 'v '; } elseif($potential) { echo '> '; }else{ echo '  '; }?><?php echo $_category->getName();?></a>
                <?php if ($open && $potential): ?>
                <?php var_dump($_category->getChildrenCategories()); ?>
                <ul>
                    <?php foreach ($_category->getChildrenCategories() as $subcategory): ?>
                    <?php $subCat = Mage::getModel('catalog/category')->load($subcategory); ?>
                    <?php $open = $this->isCategoryActive($subCat); ?>
                    <?php $potential = $subCat->hasChildren(); ?>
                    <li><a href="<?php echo $this->getCategoryUrl($subCat); ?>"<?php if($open) { echo ' class="subopen"'; } ?><?php if($potential) { echo ' class="potential"'; } ?><?php if(!$potential&&$open) { echo ' class="final"'; } ?> ><?php if($potential&&$open) { echo ':: '; } elseif($potential) { echo '> '; }?><?php echo $subCat->getName(); ?> (<?php echo $subCat->getProductCount(); ?>)</a>
                        <?php if ($open && $potential): ?>
                        <ul>
                            <?php foreach ($subcategory->getChildrenCategories() as $subsubcategory): ?>
                            <?php $subsubCat = Mage::getModel('catalog/category')->load($subsubcategory); ?>
                            <?php $open = $this->isCategoryActive($subsubCat) ?>
                            <li><a href="<?php echo $this->getCategoryUrl($subsubCat); ?>" <?php if($open) { echo ' class="final"'; } ?>><?php echo $subsubCat->getName(); ?> (<?php echo $subsubCat->getProductCount(); ?>)</a></li>
                            <?php endforeach; ?>
                        </ul>
                        <?php endif; ?>
                    </li>
                    <?php endforeach; ?>
                </ul>
                <?php endif; ?>
            </li>
            <?php } ?>
            <?php endforeach ?>
        </ul>

This line always returns true

<?php $potential = $_category->hasChildren(); ?>

And I know that the category has children.

Can anyone suggest why this doesn't work?

This is how I place the phtml in the page:

<reference name="left">
    <block type="catalog/navigation" name="catalog.vertnav" template="catalog/navigation/vert_nav.phtml" before="-" />
</reference>

Magento version 1.5.1.0

Upvotes: 0

Views: 6569

Answers (4)

Nikhil_K_R
Nikhil_K_R

Reputation: 2373

<?php $helper = $this->helper('catalog/category') ?>
 <?php $categories = $this->getStoreCategories() ?>
  <?php foreach($categories as $category): ?>
    <?php $subcategories = $category->getChildren() ?>
      <?php foreach($subcategories as $subcategory): ?>
        <?php $subsubcategories = $subcategory->getChildren() ?>
         <?php foreach($subsubcategories as $subsubcategory): ?>
         <?php endforeach; ?><!-- end foreach subsubcategories -->
      <?php endforeach; ?><!-- end foreach subcategories -->
  <?php endforeach; ?><!-- end foreach categories -->

This is the basic idea behind extracting children categories.
So accordingly you can work on it.

Upvotes: 0

magento code
magento code

Reputation: 185

Try with this code,

$this->getCurrentCategory()->getChildrenCategories()

Hope this helps

Upvotes: 0

clockworkgeek
clockworkgeek

Reputation: 37700

You might also like to try this,

foreach ($_category->getCategories($_category) as $subcategory):

getCategories() is a more complex form but it allows you more control too. By specifying a recursion level you can retrieve not just the children but grand-children too...

Upvotes: 0

pzirkind
pzirkind

Reputation: 2338

Try using $_category->getChildren() (instead of $_category->getChildrenCategories() )


Have an easy day,
Pesach

Upvotes: 4

Related Questions