0

So I have the following code in /[my-theme-name]/template/catalog/navigation/left.phtml as a proof of concept:

<?php
$Mage_Catalog_Block_Navigation = new Mage_Catalog_Block_Navigation();
$categories = $Mage_Catalog_Block_Navigation->getStoreCategories();

function render_flat_nav($categories) {
    $html = '<ul>';
    foreach($categories as $category) {
        $html .= '<li><a href="' . $category->getCategoryUrl($cat) . '">' . 
                  $category->getName() . "</a>\n";
        if($category->hasChildren()) {
            $children = $category->getChildren();
            $html .= render_flat_nav($children);
        }
        $html .= '</li>';
    }
    return $html . '</ul>';
}
echo render_flat_nav($categories); ?>

It works great for level 0 and level 1 categories but any categories that are more deeply nested are never printed out.

So $category->getChildren() can't quite be returning what I expect it to. Is there a method I can call that will work with my recursive function?

4
  • What does $category->getChildren() returns? Commented Aug 26, 2011 at 9:43
  • It returns a collection of classes, but I am assume it is different in some way to the collection returned by getStoreCategories() Commented Aug 26, 2011 at 9:53
  • Doesn't it return the children of the category? I don't know magento, try to check the document. Commented Aug 26, 2011 at 9:56
  • Maybe you can just use Mage_Catalog_Block_Navigation#renderCategoriesMenuHtml (see docs.magentocommerce.com/Mage_Catalog/…)? Why do you need an extra function for it? Commented Aug 28, 2011 at 21:17

2 Answers 2

1

I have found an answer to the problem, but it could be sub-optimal:

<?php
$Mage_Catalog_Block_Navigation = new Mage_Catalog_Block_Navigation();
$categories = $Mage_Catalog_Block_Navigation->getStoreCategories();

function render_flat_nav($categories) {
    $html = '<ul>';
    foreach($categories as $category) {
        $html .= '<li><a href="' . $category->getCategoryUrl($cat) . '">' . 
                  $category->getName() . "</a>\n";
        if($category->hasChildren()) {
            $children = Mage::getModel('catalog/category')->getCategories($category->entity_id);
            $html .= render_flat_nav($children);
            }
        $html .= '</li>';
    }
    return $html . '</ul>';
}
echo render_flat_nav($categories); ?>
Sign up to request clarification or add additional context in comments.

Comments

0
$_category = Mage::getModel('catalog/category')->load(257);    
         $_categories = $_category
                    ->getCollection()
                    ->addAttributeToSelect(array('name', 'image', 'description'))
                    ->addIdFilter($_category->getChildren());

function render_flat_nav($categories) {
    $html = '<ul>';
    foreach($categories as $category) {
        $html .= '<li><a href="' . $category->getCategoryUrl($cat) . '">' . 
                  $category->getName() . "</a>\n";
        if($category->hasChildren()) {
            $children = Mage::getModel('catalog/category')->getCategories($category->entity_id);
            $html .= render_flat_nav($children);
            }
        $html .= '</li>';
    }
    return $html . '</ul>';
}
echo render_flat_nav($_categories);

Thanks to the above i managed to get this to work for a specific category id and this works with flat categories aswell.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.