Friday 5 October 2018

How to get list of all categories array tree view in Magento 1

Call Category function

$categories = $this->getTreeCategories(2, false);
print_r($categories);
Get All categories array tree view

function getTreeCategories($parentId, $isChild) {
     
        $allCats = Mage::getModel('catalog/category')->getCollection()
                ->addAttributeToSelect('*')
                ->addAttributeToFilter('is_active', '1')
                ->addAttributeToFilter('include_in_menu', '1')
                ->addAttributeToFilter('parent_id', array('eq' => $parentId));
        $subcat = ($isChild) ? "  -- " : "";
        foreach ($allCats as $category) {
            $options[] = array(
                'label' => Mage::helper('jaydip_attrfiltercount')->__($subcat.iconv(mb_detect_encoding($category->getName(), mb_detect_order(), true), "UTF-8", $category->getName())),
                'value' => $category->getId()
            );
            $subcats = $category->getChildren();
            if ($subcats != '') {
                $options[] = $this->getTreeCategories($category->getId(), true);
            }
        }
        $optiondata = array();
        foreach ($options as $option):
            if(isset($option[0])){
             
                foreach ($option as $value):
                    $optiondata[] = array(
                        'label' => $value['label'],
                        'value' => $value['value']
                    );
                endforeach;
             
            }else{
                $optiondata[] = array(
                    'label' => $option['label'],
                    'value' => $option['value']
                );
            }
        endforeach;
     
        return $optiondata;
    }

No comments:

Post a Comment