Skip to content

Instantly share code, notes, and snippets.

@0-Sony
Last active May 6, 2016 09:03
Show Gist options
  • Select an option

  • Save 0-Sony/c911559c3d472ca287a1 to your computer and use it in GitHub Desktop.

Select an option

Save 0-Sony/c911559c3d472ca287a1 to your computer and use it in GitHub Desktop.
Get Category Tree Model for Adminhtml
<?php
class Namespace_Adminhtml_Model_Source_Category_Tree extends Mage_Core_Model_Abstract
{
/**
* @return array
* @throws Mage_Core_Exception
*/
public function toOptionArray()
{
/**
* Get all active categories
*/
$categoryCollection = Mage::getModel('catalog/category')
->getCollection()
->addAttributeToSelect('name')
->addAttributeToSort('path', 'asc')
->addFieldToFilter('is_active', array(
'eq' => '1'
))
->load()
->toArray();
/**
* Build the 'tree'
*/
$categoryTree = array();
foreach ($categoryCollection as $categoryId => $categoryData) {
if (isset($categoryData['name'])) {
$label = $categoryData['name'];
for ($i = 1; $i < $categoryData['level']; $i++) {
if ($i == 1) {
$label = '-- ' . $label;
} else {
$label = '--' . $label;
}
}
$categoryTree[] = array(
'label' => $label,
'level' => $categoryData['level'],
'value' => $categoryId
);
}
}
return $categoryTree;
}
}
<?xml version="1.0" encoding="utf-8" ?>
<config>
<tabs>
<namespace_module translate="label" module="namespace_module">
<label>Custom Label</label>
<sort_order>110</sort_order>
</namespace_module>
</tabs>
<sections>
<namespace_module translate="label" module="namespace_module">
<label>Custom Label</label>
<tab>namespace_module</tab>
<frontend_type>text</frontend_type>
<sort_order>100</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<groups>
<category translate="label">
<label>Categories Configuration</label>
<sort_order>2</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>1</show_in_store>
<fields>
<custom_tag>
<label>Custom Label</label>
<frontend_type>select</frontend_type>
<source_model>namespace_adminhtml/source_category_tree</source_model>
<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>
</custom_tag>
</fields>
</category>
</groups>
</namespace_module>
</section>
</config>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment