Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

magento programmatically build category tree

Tags:

php

magento

I want to add the category tree into select multiple options control

I search lot on this got this link

but it gives me output in ul li structure as follows

enter image description here

but I want this tree structure into select multiple options

can any one knows what to do changes in the link code

like image 580
Rohan Patil Avatar asked Feb 25 '26 20:02

Rohan Patil


1 Answers

Preparing array:

public function getCategoriesArray() {

    $categoriesArray = Mage::getModel('catalog/category')
            ->getCollection()
            ->addAttributeToSelect('name')
            ->addAttributeToSort('path', 'asc')
            ->load()
            ->toArray();

    $categories = array();
    foreach ($categoriesArray as $categoryId => $category) {
        if (isset($category['name']) && isset($category['level'])) {
            $categories[] = array(
                'label' => $category['name'],
                'level'  =>$category['level'],
                'value' => $categoryId
            );
        }
    }

    return $categories;
}

Displaying in form:

    $fieldset->addField('categories', 'multiselect', array(
        'label' => $this->__('Categories'),
        'name' => 'categories',
        'values' => Mage::getModel(...)->getCategoriesArray(),
    ));
like image 68
freento Avatar answered Feb 28 '26 12:02

freento