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
Be the first to comment.