8

right now, I have two nested foreach to loop through all the category, and it's extremely slow (3-4s on my local server). I think the problem is i use ->LOAD() to get the image path for the child category. is there any other way of doing it? or I should just redesign the whole navigation ui? any suggestion?

$categories = Mage::getSingleton('catalog/category')
    ->getCollection()
    ->addAttributeToSelect('id')
    ->addAttributeToSelect('name')
    ->addAttributeToSelect('url_key')
    ->addAttributeToSelect('url')
    ->addAttributeToFilter('level',2)
    ->addIsActiveFilter()
    ->addAttributeToSort('position');

foreach ($categories as $category): 
    $id = $category->getId();
    $children = $category->getChildrenCategories();
    $name = $category->getName();
    $url_path = $category->getUrl();        

    foreach ($children as $child):                                      
        $childId = $child->getId();
        $thisChild = Mage::getSingleton('catalog/category')->load($childId);
        $name = $thisChild->getName();
        $url_key = $thisChild->getUrlKey(); 
        $url_path = $child->getUrl();
        $img = $thisChild->getImageUrl();   
    endforeach;
endforeach;
3

I don't know whether this even works, but you can try to work with Mage_Catalog_Model_Resource_Category_Tree

This class should give you the possibility to traverse the tree, but after loading all the categories.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.