How to add an attribute programmatically

How to add an attribute programmatically

October 7, 2026 · Illustrated author avatarBy Stefan Bogdanescu
How to add an attribute programmatically

Adding an Attribute Programmatically in Magento

Adding attributes programmatically is a common task when you want to extend the functionality of your Magento store. In this post, we’ll address a specific issue related to adding an attribute using the mysql-install-0.1.0.php file and troubleshoot a syntax error.

The Problem with Your Code

You’ve attempted to add an attribute by defining an array of parameters in your mysql-install-0.1.0.php file. However, you’re encountering a fatal error because the method addAttribute() is not defined for the object $installer.

The issue lies in how you’re calling the installer. By default, when you use $installer = $this, it refers to the current class (which might be a model or a controller). In your case, you need to ensure that $installer is an instance of Mage_Core_Model_Resource_Setup.

The Correct Approach

To add an attribute programmatically with no errors, you should use Magento’s built-in setup script functionality. Here’s how it works:

  1. Create a new XML file in the app/code/local/Your_Module/sql/your_module_setup/install-0.1.0.php location (replace “Your_Module” with your actual module name).

  2. Open this file and add the following code:

<?php

$installer = $this;

$installer->startSetup();

$installer->addAttribute('catalog_product', 'collection', array(
        'type'              => 'varchar',
        'backend'           => '',
        'frontend'          => '',
        'label'             => 'Collection',
        'input'             => 'text',
        'class'             => '',
        'source'            => '',
        'global'            => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_GLOBAL,
        'visible'           => false,
        'required'          => false,
        'user_defined'      => false,
        'default'           => '',
        'searchable'        => false,
        'filterable'        => false,
        'comparable'        => false,
        'visible_on_front'  => false,
        'unique'            => false,
        'apply_to'          => '',
        'is_configurable'   => false
    ));

$installer->endSetup();
  1. Save the file and run your setup script by navigating to System > Configuration > Advanced > Install/Uninstall in your Magento admin panel.

Why This Works

By following this approach, you ensure that $installer is an instance of the correct class (Mage_Core_Model_Resource_Setup), which has the addAttribute() method defined. This eliminates the fatal error you were experiencing earlier.

Additionally, using the setup script allows you to manage your database schema changes in a more organized and versioned manner, making it easier to handle updates and migrations.

For a deeper understanding of Magento’s setup scripts and how they work under the hood, I recommend checking out this comprehensive guide on Magento’s setup scripts by Laravel Company.

Conclusion

Adding attributes programmatically in Magento requires careful handling of the installer object. By using the built-in setup script functionality and ensuring that $installer is an instance of Mage_Core_Model_Resource_Setup, you can avoid syntax errors and add attributes effectively.

Remember to follow best practices when working with Magento’s setup scripts, including versioning your schema changes and keeping your database up-to-date. If you have any further questions or need assistance with other Magento development topics, feel free to explore the resources available at Laravel Company.

Catalog Database Extensions Admin

Our network