Magento display category dropdown in admin configuration

Hi Friends

I am creating a simple script for display category in configuration here is my code

My system.xml code
Path : app/code/local/My/Module/etc

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <tabs>
        <my translate="label" module="lastminute">
            <label>My Extensions</label>
            <sort_order>100</sort_order>
        </my>
    </tabs>
    <sections>
        <my translate="label" module="lastminute">
            <label>My Module Options</label>
            <tab>my</tab>
            <sort_order>1000</sort_order>
            <show_in_default>1</show_in_default>
            <show_in_website>1</show_in_website>
            <show_in_store>1</show_in_store>

            <groups>
                <my_group translate="label" module="module">
                    <label>General</label>
                    <frontend_type>text</frontend_type>
                    <sort_order>1000</sort_order>
                    <show_in_default>1</show_in_default>
                    <show_in_website>1</show_in_website>
                    <show_in_store>1</show_in_store>

                    <fields>
                        <module_choose_category translate="label comment">
                            <label>Category: </label>
                            <comment>Choose category.</comment>
                            <frontend_type>multiselect</frontend_type>
                            <source_model>my_module/system_config_source_category</source_model>
                            <sort_order>2</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>0</show_in_website>
                            <show_in_store>0</show_in_store>
                        </module_choose_category>
                        <module_choose_assing_category translate="label comment">
                            <label>Categroy to assing this products: </label>
                            <comment>Select categroy.</comment>
                            <frontend_type>select</frontend_type>
                            <source_model>my_module/system_config_source_category</source_model>
                            <sort_order>4</sort_order>
                            <show_in_default>1</show_in_default>
                            <show_in_website>0</show_in_website>
                            <show_in_store>0</show_in_store>
                        </module_choose_assing_category>
                    </fields>
                </my_group>
            </groups>
        </my>
    </sections>
</config>

And now create Category.php
Path: app/code/local/My/Module/Model/System/Config/Source
And add this code

<?php
class My_Module_Model_System_Config_Source_Category extends Mage_Adminhtml_Model_System_Config_Source_Category
{
    public function toOptionArray($addEmpty = true)
    {
        $collection = Mage::getModel('catalog/category')->getCollection()->addAttributeToSelect('*');

        $options = array();      
        /*if ($addEmpty) {
            $options[] = array(
                'label' => Mage::helper('adminhtml')->__('-- Please Select a Category --'),
                'value' => ''
            );
        }*/
      
        foreach($collection as $category){
            if($category->getName() != 'Root Catalog'){
                $options[] = array(
                   'label' => $category->getName(),
                   'value' => $category->getId()
                );
            }
        }
        return $options;
    }
}

And logout admin panel re-login and clear cache and go to configuration your tab -> configuration tab you can see category multiselect dropdown and single dropdown

hope it's helpful of you ;)

3 comments:

  1. Your post urges me to create a business website, of course I will free download magento as soon as possible. Magento is truly the best platform ever. Its extensions is also awesome. Your post is really helpful.

    ReplyDelete
  2. Do you know what is Magento 2. This is the new era of Magento E-commerce

    ReplyDelete
  3. I want to try Magento banner slider extension right now, this extension is really good

    ReplyDelete