1

I've created a module that has an option to select the categories which should have the module enabled to. For this I'm using a multiselect option and is created in my system.xml as follows:

<includecats>
  <label>Select categories</label>
  <frontend_type>multiselect</frontend_type>
  <source_model>admininfo/categories</source_model>
  <sort_order>30</sort_order>
  <show_in_default>1</show_in_default>
  <show_in_website>1</show_in_website>
  <show_in_store>1</show_in_store>
  <comment>Select categories you would like to include. Use Command + Click (Mac) or Ctrl + Click (Windows).</comment>
</includecats>

In "Model/Categories.php" I have included:

<?php
class Mymodule_admininfo_Model_Categories extends Mage_Core_Model_Abstract {

public function toOptionArray()
{
$categoriesArray = Mage::getModel('catalog/category')
    ->getCollection()
    ->addAttributeToSelect('name')
    ->addAttributeToSelect('url_path')
    ->addAttributeToSort('name', 'asc')
    ->addFieldToFilter('is_active', array('eq'=>'1'))
    ->load()
    ->toArray();

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

return $categories;
} 
}

This all works like a charm. However in my module I get a list of all categories without any reference to its possible parent categories. This makes this list extremely hard to use since some categories used several times in the catalog. Without a reference to a parent or the actual id of the category it's pretty much unusable.

Current situation (example):

  • Shoes
  • Shoes
  • Shoes

Desired situation:

  • Men | Shoes
  • Women | Shoes
  • Childeren | Shoes

Or:

  • Shoes (9)
  • Shoes (10)
  • Shoes (11)
4

We had the same issue with a hackathon module. This is the solution we came up with. It defines the path per category separated with /

class Mymodule_admininfo_Model_Categories extends Mage_Core_Model_Abstract {

    public function toOptionArray()
    {
        $categories = array();
        $allCategoriesCollection = Mage::getModel('catalog/category')
            ->getCollection()
            ->addAttributeToSelect('name')
            ->addFieldToFilter('level', array('gt'=>'0'));
        $allCategoriesArray = $allCategoriesCollection->load()->toArray();
        $categoriesArray = $allCategoriesCollection
            ->addAttributeToSelect('level')
            ->addAttributeToSort('path', 'asc')
            ->addFieldToFilter('is_active', array('eq'=>'1'))
            ->addFieldToFilter('level', array('gt'=>'1'))
            ->load()
            ->toArray();
        foreach ($categoriesArray as $categoryId => $category)
        {
            if (!isset($category['name'])) {
                continue;
            }
            $categoryIds = explode('/', $category['path']);
            $nameParts = array();
            foreach($categoryIds as $catId) {
                if($catId == 1) {
                    continue;
                }
                $nameParts[] = $allCategoriesArray[$catId]['name'];
            }
            $categories[$categoryId] = array(
                'value' => $categoryId,
                'label' => implode(' / ', $nameParts)
            );
        }

        return $categories;
    } 
}
  • That is a very handsome solution and indeed works like a charm. Thank you Sander! – Badger Dec 8 '15 at 16:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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