Magento: Add product attributes with module installation script

Magento: Add product attributes with module installation script

August 26, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento: Add product attributes with module installation script

Magento: Correctly Adding Product Attributes via Module Installation Script

When creating custom attributes for your Magento products, you’ll need to ensure that your installation script is properly configured to add these attributes to all existing products. In this post, we’ll troubleshoot the issue you’re facing and provide a corrected approach using the setup/installSchema file.

Understanding the Issue with Your Current Setup

The main problem lies in the configuration of your config.xml file under the <adminhtml> node. You’ve correctly defined the access levels for the setup, write, and read resources but missed an important attribute that’s necessary for the installation script to work properly: setup_module.

Your current config.xml:

<adminhtml>
    <acl>
        <module_setup>
            ...
        </module_setup>
        ...
    </acl>
</adminhtml>

Correcting Your config.xml File

To fix this issue, you need to add the <setup_module> node inside the config tag of your module. This tells Magento which file should be used for installing or upgrading the database schema.

Corrected config.xml:

<config>
    <modules>
        <My_Module>
            <version>1.0.0</version>
        </My_Module>
    </modules>
    ...
</config>

Updating Your setup/installSchema File

Next, let’s update the content of your setup/installSchema file to correctly add the attribute.

Corrected setup/installSchema file:

<?php

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

$installer->run("
    CREATE TABLE IF NOT EXISTS {$installer->getTable('catalog_product_entity_int')} (
      entity_id int(10) unsigned NOT NULL auto_increment,
      attribute_id int(10) unsigned NOT NULL default 0,
      store_id smallint(5) unsigned NOT NULL default 0,
      value_text varchar(255) default NULL,
      value_textarea text default NULL,
      value_int int(11) default NULL,
      value_decimal(12,4) default NULL,
      value_datetime datetime default NULL,
      value_timestamp timestamp default NULL,
      value_date date default NULL,
      value_boolean tinyint(1) unsigned default NULL,
      entity_type_id varchar(32) NOT NULL default 0
    ) ENGINE=InnoDB COMMENT='Product Entity Int Table'
");

$installer->endSetup();

Step-by-Step Guide to Adding Attributes

To add a new attribute via the installation script, follow these steps:

  1. Create an eav/attribute record in your database for the new attribute.
  2. Use the setup/installSchema file to create any necessary tables required by the attribute (in this case, catalog_product_entity_int).
  3. Ensure that your config.xml is properly configured with the <setup_module> node.

Conclusion

In conclusion, adding product attributes via a Magento module installation script requires careful configuration of both the config.xml file and the setup/installSchema file. By following the corrected approach outlined above, you’ll be able to successfully add new attributes to all existing products in your store.

Remember that properly configuring your installation scripts ensures a smooth upgrade process for future versions of your module. If you’re looking for more in-depth guidance on Magento development best practices and advanced features, check out our comprehensive guides at Laravel Company.

Catalog Database Extensions Admin

Our network