0
function productDescription()
{
    $_helper = Mage::helper('catalog/category');
    $category = Mage::getModel('catalog/category')->load($categoryId);
    $_categories = $category->getChildrenCategories() 
    print_r($_categories);
    $array = json_encode($_categories);
    file_put_contents("product_node.json", $array);
}
3
  • print_r($options); what is value in variable $options ? Commented Jul 7, 2017 at 11:44
  • How do i get this? if i want only subcategories of perticular category Commented Jul 7, 2017 at 11:49
  • You have missing semicolon in line $_categories = $category->getChildrenCategories() otherwise your code run perfect I have test it. or once try to echo $categoryID and check if that category has sub categories or not. Commented Jul 7, 2017 at 13:19

4 Answers 4

1

You can try this:

public function getCategories()
{
    $categoryId = 2;
    $categoryTree = Mage::getResourceModel('catalog/category')->getCategories($categoryId);

    $array = array();
    foreach ($categoryTree as $category) {
        $array[$category->getPathId()] = $category->getName();
        $array = array_merge($array, $this->_getChildren($category));
    }
    var_dump($array);
}

public function _getChildren($category, $array = array())
{
    if ($category->getChildrenCount()) {
        foreach ($category->getChildren() as $child) {
            $array[$child->getPathId()] = $child->getName();
            $this->_getChildren($child, $array);
        }
    }
    return $array;
}

Output looks like:

array(
    '1/2/5' => 'Men',
    '1/2/5/14' => 'New Arrivals',
    '1/2/5/15' => 'Shirts',
    ...
);
3
  • I need Category id should be dynamic, so that in listing page, i will get only subcategories of that page's category in sidebar. Commented Jul 8, 2017 at 4:55
  • Replace $categoryId = 2; with $categoryId = Mage::registry('current_category')->getId().
    – sv3n
    Commented Jul 8, 2017 at 9:25
  • Thanx sv3n, i want to create event observer for that, so that if i add product every time its need to generate an array or json Commented Jul 8, 2017 at 9:53
1

Try like this

$children = Mage::getModel('catalog/category')->getCategories(10);
foreach ($children as $category) {
    echo $category->getName();
}
3
1

Please use the folowing code :

<?php $_helper = Mage::helper('catalog/category') ?>
<?php $categoryId = 3;?>
<?php $category = Mage::getModel('catalog/category')->load($categoryId) ?>
<?php $_categories = $category->getChildrenCategories() ?>

<?php if (count($_categories) > 0): ?>
    <ul>
        <?php foreach($_categories as $_category): ?>
            <li>
                <a href="<?php echo $_helper->getCategoryUrl($_category) ?>"><?php echo $_category->getName() ?></a>
                <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
                <?php if($_category->hasChildren()):?>
                <?php $_subcategories = $_category->getChildrenCategories() ?>
                    <ul>
                        <?php foreach($_subcategories as $_subcategory): ?>
                            <li>
                                <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>"><?php echo $_subcategory->getName() ?></a>
                                <?php $_category2 = Mage::getModel('catalog/category')->load($_subcategory->getId()) ?>
                                <?php if($_category2->hasChildren()):?>
                                <?php $_subcategories2 = $_category2->getChildrenCategories() ?>
                                    <ul>
                                        <?php foreach($_subcategories2 as $_subcategory2): ?>
                                            <li>
                                                <a href="<?php echo $_helper->getCategoryUrl($_subcategory2) ?>"><?php echo $_subcategory2->getName() ?></a>
                                            </li>
                                        <?php endforeach; ?>
                                    </ul>
                                <?php endif; ?>

                            </li>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

Source from How get all subcategories of specific category?

0

Try the below code:-

<?php
    //gets all sub categories of parent category 'Brands'
     $cate_id = Mage::getModel('catalog/layer')->getCurrentCategory()->getId();

    $cats = Mage::getModel('catalog/category')->load($cate_id)->getChildren();
    $catIds = explode(',',$cats);
    //print_r ($catIds);
    $categories = array();
    foreach($catIds as $catId) {
        $category = Mage::getModel('catalog/category')->load($catId); 
        $categories[$category->getName()] = array(
            'url' => $category->getUrl(),
            'img' => $category->getImageUrl(),
            'name'=>$category->getName(),
            'thumb'=>$category->getThumbnail(),
            'id'=>$category->getId()
        );
    }
    ksort($categories, SORT_STRING);
?>
<?php if (Mage::getModel('catalog/category')->load($cate_id)->getChildren()):?>
    <ul class="subs_cats row">
        <?php //print_r($catIds); ?>
        <?php foreach($categories as $name => $data): ?>

            <li class="sub_cate span6" style="text-align:center;font-size: 15px;font-weight:bold;">
                <div class="cate_img img-responsive">
                <?php if($data['thumb'] ==''): ?>
                <a href="<?php echo $data['url'];?>" title="<?php echo $data['name'];?>" alt="<?php echo $data['name'];?>"><img class="cat-ge" src="<?php echo $this->getSkinUrl('images/no_preview.png') ?>"></img></a>
                    <?php else: ?>
                       <a href="<?php echo $data['url'];?>" title="<?php echo $data['name'];?>" alt="<?php echo $data['name'];?>"><img class="cat-image" src="<?php echo Mage::getBaseUrl('media').'catalog/category/'.$data['thumb']; ?>" ></img></a>
                    <?php  endif; ?>
                </div>
                <div class="main_cate">

                <?php //echo $data['name']; ?>

                <?php if(strlen($data['name']) >=22):?>
                <a href="<?php echo $data['url'];?>" title="<?php echo $data['name'];?>" alt="<?php echo $data['name'];?>"><?php echo substr($data['name'],0, 22).' ...'; ?></a>
                <?php else:?>
                <a href="<?php echo $data['url'];?>" title="<?php echo $data['name'];?>" alt="<?php echo $data['name'];?>"><?php echo $data['name'];?></a>
                <?php endif;?>

                </div>
            </li>
        <?php endforeach; ?>
    </ul>
    <?php else:?>
        <p class="note-msg"><?php echo $this->__('There are no Subcategories for this category.');?></p>
<?php endif;?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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