Oh snap! You are using an old version of browser. Update your browser to get new awesome features. Click for more details.

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 ;)

8 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
  4. Hi I use this code for get category list in admin panel but 404 Error, Page Not Found Error are occurred Please help me.

    ReplyDelete
  5. thank you noticeably a lot for writing such a fascinating article on this concern. This has surely made me think and that i pressure to feasible more Magento webshop laten maken

    ReplyDelete
  6. FREE Express Shipping by DHL - We used DHL that delivers in 3-4 days in the US, Canada, Australia, Japan, and the countries located in Western Europe. The delivery time is around a week for Eastern Europe, South Korea, and other countries. Custom Iron On Patches

    ReplyDelete
  7. this post drives the need of getting involved in new tech, great work with the post really helpful, I would like to talk about website Visit W3Schools.com! and how you can use them to run your business smoothly , have at it!

    ReplyDelete
  8. A great user guide for Tech guys, Really easy to understand and implement , please give magento development services a go for innovative business solutions.

    ReplyDelete