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)
Best Answer
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
/