How to add custom dropdown attribute or field to magento category section

In this tutorial I am explaining how to add custom select\dropdown attribute to the magneto category section as show below.

Custom category attribute magento

Step by step explanation

  1. Create the File structure
  2. Create the module
  3. Enable that from the magento backend
  4. Create the config.xml and the sql install file
  5. Create the Model for custom options
  6. Retrieve the value in the frontend
  7. Upgrade the plugin

Create the File structure

As an initial step create the files as shown below.
 file structure for custom category attribute
Magento custom attribute file structure

Create the module

After that we need to enable the module for that create the file in app/etc/module/Addpeople_CustomFeaturedAttribute.xml folder and enter the code as shown below.

<?xml version="1.0"?>
<config>
    <modules>
      <Addpeople_CustomFeaturedAttribute>
         <active>true</active>
         <codePool>local</codePool>
      </Addpeople_CustomFeaturedAttribute>
    </modules>
</config>

Enable module from the magento backend

For enabling the module please login to the magento backend and navigate to the System/Configuration/Advanced/ and enable the module as shown and save the settings.
Enable magento module

Create config and sql file

As the next step add the config and the sql files

<?xml version="1.0"?>
<config>
    <modules>
        <Addpeople_CustomFeaturedAttribute>
            <version>0.0.2</version>
        </Addpeople_CustomFeaturedAttribute>
    </modules>
 
    <global>
        <models>
            <customfeaturedattribute>
                <class>Addpeople_CustomFeaturedAttribute_Model</class>
            </customfeaturedattribute>
        </models>
        <resources>
            <add_category_attribute>
                <setup>
                    <module>Addpeople_CustomFeaturedAttribute</module>
                    <class>Mage_Catalog_Model_Resource_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </add_category_attribute>
            <add_category_attribute_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </add_category_attribute_write>
            <add_category_attribute_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </add_category_attribute_read>
        </resources>
    </global>
</config>

<?php
$this->startSetup();
$this->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'custom_dropdown', array(
    'group'         => 'General Information',
    'input'         => 'select',
    'type'          => 'text',
    'label'         => 'Custom Dropdown',
    'backend'       => '',
    'visible'       => true,
    'required'      => false,
    'visible_on_front' => true,
    'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'source' => 'customfeaturedattribute/source_custom',
));
 
$this->endSetup();

Create the Model for custom options

Inside the sql file we mentioned the source as customfeaturedattribute/source_custom so we need create the custom.php file inside the source folder and declare the option in the model file as shown.

<?php

class Addpeople_CustomFeaturedAttribute_Model_Source_Custom extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
    public function getAllOptions()
    {
        $options = array(
            1 => 'One',
            2 => 'Two',
            3 => 'Three'
        );
 
        return $options;
    }
}

Then clear the cache and login to the category section we get the custom attribute dropdown .
Custom category attribute magento

Retrieve the value in the frontend

For displaying the values in the template please enter the following code.

<?php echo $_helper->categoryAttribute($_category, $_category->getCustomDropdown(), 'custom_dropdown') ?>

Upgrade the plugin

For upgrading the plugin please create the upgrade file (mysql4-upgrade-0.0.1-0.0.2.php) and make the necessary changes. I have changed the label.

<?php
$this->startSetup();
$this->addAttribute(Mage_Catalog_Model_Category::ENTITY, 'custom_dropdown', array(
    'group'         => 'General Information',
    'input'         => 'select',
    'type'          => 'text',
    'label'         => 'Updated Custom Dropdown',
    'backend'       => '',
    'visible'       => true,
    'required'      => false,
    'visible_on_front' => true,
    'global'        => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
    'source' => 'customfeaturedattribute/source_custom',
));
 
$this->endSetup();

Also change the version number in the config.xml file and clear the cache and refresh the category page.
upgrade

Then you will get the following result.

updated_custom_attribute

If you have any further queries please leave a comment.

Thanks

Share this Tutorial

3 comments on “How to add custom dropdown attribute or field to magento category section

    • pearl on

      Please create custom.php inside the source folder. I have mentioned source.php in my tutorial which is typo. So i am going to edit that section.

      If you have any further queries please leave a reply.

      Thanks

      pearl

      Reply
  1. rupal pinge on

    Hi,

    The code is really helpful.

    How to access the value of category custom attribute in theme’s top menu .phtml file or left sidebar navigation menu (left.phtml) ?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

*