Monday 25 July 2016

Get Category and subcategory ids array in Magento

Hi with the below code you will get the array of parent child categories.



$rootcatId  = Mage::app()->getStore()->getRootCategoryId();
    $root_cat   = Mage::getModel('catalog/category')->load($rootcatId);
    $categories = $this->get_child_categories($root_cat);
    echo "<pre>";
    print_r($categories);
    echo "</pre>";


    function get_child_categories($parent) {
        $cat_model = Mage::getModel('catalog/category');
        $categories = $cat_model->load($parent->getId())->getChildrenCategories();
        $ret_arr = array();
        foreach ($categories as $cat)
        {
            $ret_arr[] = array(
                                'cat_id' => $cat->getId(),
                                'cat_name' => $cat->getName(),
                                'cat_url' => $cat->getUrl(),
                                'child_cats' => $this->get_child_categories($cat),
                                );
        }
        return $ret_arr;

    }

No comments:

Post a Comment