Reading list Switch to dark mode

    Magento subcategories of a Specific category

    Updated 5 December 2012

    Magento subcategories of a specific  category : This is very useful and important code when you are dealing with customizing  magento . There are two ways to display subcategories in phtml file using magento models

    Solution One :  Using getCategories() Method

    $_categories = Mage::getModel('catalog/category')
                       ->getCategories(113); // 113 is category ID

    here is the rest of the code (This code is from our snippet so you may ignore some of them)

    <select id="category">
    <?php if (count($_categories) > 0): ?>
    <?php foreach($_categories as $_category): ?>
    <option  name="<?php echo $_category['entity_id']?>" 
    value="<?php echo $_category['entity_id']?>">
    <?php echo $_category->getName();?></option>
    <?php endforeach; ?>
    <?php endif; ?>									
    </select>

    Solution Two: using Load() getChildren() Method .

    $root = Mage::getModel('catalog/category')->load(66); // Put your category ID here.
    $subCat = explode(',',$root->getChildren()); 
    //get the collection add filter using the exploded categories
    $collection  = $root
                 ->getCollection()
                 ->addAttributeToSelect("*")
                 ->addFieldToFilter("entity_id", array("in", $subCat) );

    and to display in them use the same code as above . enjoy magento coding

    Searching for an experienced
    Magento Company ?
    Find out More
    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home