1

How can I get a collection of the categories and subcategories to build custom navigation ?

$_categories = Mage::getResourceModel('catalog/category_collection');

$i;

echo '<div class="navigation">';

foreach($_categories as $_category){

    echo '<ul class="category">';

    echo $i. '<li>'. $_category->getName().'</li>';$i++;

    echo $categoryId = $_category->getId();

    $_category = Mage::getModel('catalog/category')->load($categoryId);

//$category = Mage::getModel('catalog/category')->load(228);

    $subcategories = $_category->getChildrenCategories()->addAttributeToSelect('*');

    if (count($subcategories) > 0){

        echo $_category->getName();

        foreach($subcategories as $id){

            echo $name = $id->getName().'<br/>';
        }
    }
}
echo '</div>';`

It actually is giving me a full list of categories including all those I don't need ;) Should I pull all categories from products ?

  • Please add details which categories you don't want in your collection – Jaimin Sutariya Mar 14 '17 at 17:56
  • Actually i would like to have all categories and sub categories then i will seletcively show/hide what i want to see – Rob D. A. Mar 14 '17 at 19:17
2

you can select sub categories based on categorie id also. If You need i can give you code also .

// here is the code

<?php 

$parentCategoryId = 222;
$cat = Mage::getModel('catalog/category')->load($parentCategoryId);
$subcats = $cat->getChildren();

// Get 1 Level sub category of Parent category
foreach(explode(',',$subcats) as $subCatid)
              {
                $_category = Mage::getModel('catalog/category')->load($subCatid);
  if($_category->getIsActive()) {
    echo '<li><a href="'.$_category->getURL().'" title="View the products for the "'.$_category->getName().'" category">'.$_category->getName().'</a><li>';

  }
}
*/
?>
  • Hello Saurabh, would be very nice. However I was doing it, just because collection contains all categories including brands, events etc not just products ... my menu is like 800 buttons :) and I don't know how to filter it :) – Rob D. A. Mar 15 '17 at 12:12
  • <?php $parentCategoryId = 222; // change to your main cat id $cat = Mage::getModel('catalog/category')->load($parentCategoryId); $subcats = $cat->getChildren(); // Get 1 Level sub category of Parent category foreach(explode(',',$subcats) as $subCatid) { $_category = Mage::getModel('catalog/category')->load($subCatid); if($_category->getIsActive()) { echo '<li><a href="'.$_category->getURL().'" title="View the products for the "'.$_category->getName().'" category">'.$_category->getName().'</a><li>'; } } */ ?> – inrsaurabh Mar 15 '17 at 12:19
  • Aaaaaa it's obvious! Silly me. You are 100% right, this is it!. I should read only a root for this range of categories what in result will give me an array with ids I'm looking for ;) Right ? :) – Rob D. A. Mar 15 '17 at 12:22
  • Yes brother ! You can fetch $_category as you want . – inrsaurabh Mar 15 '17 at 12:24

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.