3

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

1 Answer 1

5

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(),
    ));
7
  • thanks for this but how can I use addField() into my custom form I want to assign the category tree structure in <select> with multiple control Commented Feb 2, 2013 at 10:04
  • Use (foreach(Mage::getModel(...)->getCategoriesArray() as $category)) and create your multiselect manually.
    – freento
    Commented Feb 2, 2013 at 10:12
  • ok thanks can I get the category level using getCategoriesArray() function? Commented Feb 2, 2013 at 10:14
  • yes. add $category['level'] to categories array. Have a look - I changed answer
    – freento
    Commented Feb 2, 2013 at 10:16
  • sorry but how to get only active categories using getCategoriesArray() function? Commented Feb 2, 2013 at 11:26

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.