Wednesday, 6 June 2012

display all subcategory of main category in magento

 <?php

 $root_category = Mage::getModel('catalog/category')->load(3);  
// Put your root category ID here.
 
$subcategories = $root_category->getChildren();
foreach(explode(',',$subcategories) as $subcategory) {
$category = Mage::getModel('catalog/category')->load($subcategory);
echo '<a href="'.$category->getURL() .'" />'.$category->getName().'</a><br/>';
  }

  ?>
 
 
 Particular Category And Subcategory Not Show in  Category Menu:--
 
 <h3>Category</h3>
 <?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
    <ul>
        <?php foreach($_categories as $_category): ?>
            <li>
   <?php if (!in_array($_category->getId(), array(10))) : ?>
                <a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
                    <?php echo $_category->getName() ?>
                </a>
    <?php endif; ?>
                <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
                <?php $_subcategories = $_category->getChildrenCategories() ?>
                <?php if (count($_subcategories) > 0): ?>
                    <ul>
                        <?php foreach($_subcategories as $_subcategory): ?>
                            <li>
                                <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
                                    <?php echo $_subcategory->getName() ?>
                                </a>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>
            </li>
        <?php endforeach; ?>
  
    </ul>
<?php endif; ?>

2 comments:

  1. How to display categories in dropdown list, and when select catgegory, category subcategory shold display accordingly in another dropdown.

    Thanks

    ReplyDelete
  2. Great blog. All posts have something to learn. Your work is very good and i appreciate you and hoping for some more informative posts.keep writing
    magento development company in bangalore 

    ReplyDelete

Note: only a member of this blog may post a comment.