Category list for custom magento system configuraion section



1) Let's create a configuration file
Location: app\code\community\Namespace\Module\etc\system.xml
<config>
<tabs>
<inchoo module="inchoo_notes">
<label>Namespace</label>
<sort_order>110</sort_order>
</inchoo>
</tabs>
<sections>
<module translate="label" module="module">
<class>separator-top</class>
<label>Featured Category</label>
<tab>inchoo</tab>
<sort_order>10</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<groups>
<fc translate="label">
<label>Home Page Featured Category</label>
<frontend_type>text</frontend_type>
<sort_order>1</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<fields>
<active translate="label comment">
<label>Featured Category is active:</label>
<frontend_type>select</frontend_type>
<source_model>adminhtml/system_config_source_yesno</source_model>
<sort_order>0</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</active>
<category>
<label>Category</label>
<comment>Select category name to dispaly in home page.</comment>
<frontend_type>multiselect</frontend_type>
<source_model>module/system_config_source_category</source_model>
<sort_order>1</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
</category>
</fields>
</fc>
</groups>
</module>
</sections>
</config>

2) Create a model to get category list
<?php
class Namespace_Module_Model_System_Config_Source_Category
{
public $optArr = array();
/*
* Prepare data for System->Configuration dropdown
* */
public function toOptionArray()
{
$category = Mage::getModel('catalog/category');
$tree = $category->getTreeModel();
$tree->load();
$ids = $tree->getCollection()->addAttributeToFilter('level','2')->getAllIds(); // get sub category of root category(default category)
$catArr = array();
foreach($ids as $id){
$catObj = Mage::getModel('catalog/category')->load($id);
$this->optArr[] = $this->getArr($catObj);
$firChild = $catObj->getChildren();
$this->getSubCat($firChild);
}
return $this->optArr;
}
public function getArr($category){
$depth = count(explode('/', $category->getPath())) - 2;
$indent = str_repeat('-', max($depth * 2, 0));
$options = array(
'label' => $indent . $category->getName(),
'value' => $category->getId()
);
return $options;
}
public function getSubCat($subCat){
foreach(explode(',',$subCat) as $catId)
{
$sCat = Mage::getModel('catalog/category')->load($catId);
if($sCat->getIsActive())
{
$sCatName = $sCat->getName();
$this->optArr[] = $this->getArr($sCat);
}
$secChild = $sCat->getChildren();
if($secChild != ''){
$this->getSubCat($secChild);
}
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s