Drop down attribute in category - Magento

Drop down attribute in category - Magento

August 13, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Drop down attribute in category - Magento

Magento: Creating a Drop-down Attribute for Categories

Creating a new attribute for your Magento categories can be a straightforward process if you follow the right steps. Recently, one of our clients asked us how to create a drop-down menu in their category attributes using an installer. The client provided some code snippets that attempted to achieve this but faced challenges in implementing it correctly.

Understanding the Client’s Attempt

The client tried to add a new attribute named ‘priority’ with a drop-down option in their category setup (installer). The code included:

  1. Adding the attribute using addAttribute method.
  2. Setting default options for the drop-down menu.
  3. Inserting data into the catalog_category_entity_int table.
  4. Loading and saving category models.

However, when they attempted to view this in the Magento backend under Catalog > Categories > Manage Categories, nothing showed up. They could only create a text field attribute, which was not their desired outcome.

The Correct Approach

To address this issue effectively, let’s break down the process into clear steps:

Step 1: Add the Attribute

The first step is to add the attribute correctly using the addAttribute method. Here’s how you can modify your code:

$installer = $this;
$installer->startSetup();

$entityTypeId = $installer->getEntityTypeId('catalog_category');
$attributeSetId = $installer->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

$installer->addAttribute('catalog_category', 'priority', array(
    'type' => 'varchar',
    'label' => 'Priority',
    'input' => 'select',
    'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
    'visible' => true,
    'required' => false,
    'user_defined' => false,
    'source_model' => 'custom_module/options',  // Add source model
    'default' => array(0)
));

$installer->addAttributeToGroup(
        $entityTypeId, $attributeSetId, $attributeGroupId, 'priority', '11'
);

$installer->endSetup();

Note the addition of the source_model parameter with the value 'custom_module/options'. This is crucial for creating a drop-down menu.

Step 2: Create the Options Source

Next, you need to create a source model that will define the options available in your drop-down. In this case, we’ll use a custom module named ‘custom_module’. Here’s how to set it up:

  1. In your app/code/local/custom/module/etc/config.xml file, add the following lines under <modules>:
<options>
    <class>Custom_Module_Model_Option</class>
</options>
  1. Create a new file Option.php in the app/code/local/custom/module/Model/ folder with the following content:
<?php
class Custom_Module_Model_Option extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
    public function getAllOptions()
    {
        $options = array(
            array('label' => '', 'value' => 0),
            array('label' => 'Normal', 'value' => 1),
            array('label' => 'Grouping', 'value' => 2),
            array('label' => 'Highlighted', 'value' => 3),
            array('label' => 'Trendy', 'value' => 4),
        );

        return $options;
    }
}

This getAllOptions method will be used to populate your drop-down menu with the specified options.

Step 3: Verify Your Changes

After implementing these changes, clear all cache and refresh your Magento backend. You should now see a new attribute named ‘Priority’ in your category setup that allows you to select one of the defined options from a drop-down list.

Conclusion

Creating a drop-down attribute for Magento categories is possible by following the correct steps outlined above. It’s crucial to set up the source model and create the getAllOptions method correctly.

Remember, when working with Magento installation scripts or custom attributes, always ensure you have a backup of your data and follow best practices to avoid any potential issues.

If you need further assistance with Magento development or want to explore advanced features like category-based pricing rules, feel free to contact us at Laravel Company. We specialize in providing top-notch Magento solutions tailored to meet the unique needs of your business.

Happy coding!

Catalog Database Admin Configuration

Our network