Adding custom attribute to product in Magento 2.3.3 programmatically

Adding custom attribute to product in Magento 2.3.3 programmatically

September 17, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Adding custom attribute to product in Magento 2.3.3 programmatically

Adding Custom Attributes to Products in Magento 2.3.3 Programmatically

Adding custom attributes to products programmatically is a common need when developing extensions for Magento. This process involves creating an upgrade data script that defines the new attribute and installing it using the setup:upgrade command.

Understanding the Problem

In your provided example, you have created an UpgradeData.php class within a module named Mageplaza\HelloWorld. The script successfully uses the EAV (Entity-Attribute-Value) setup factory to add a new text attribute called ‘sample_attribute’ to the catalog product entity.

The code appears correct, and the attribute is configured with reasonable defaults for most properties. However, when you run the php magento setup:upgrade command in your terminal, the script doesn’t seem to have any effect. This could indicate that there are several potential issues at play.

Potential Issues and Solutions

1. Incorrect Module Registration

Before we dive into the code itself, let’s first ensure your module is properly registered in app/etc/config.php. If it isn’t configured correctly, Magento won’t recognize or execute its setup scripts during upgrade operations.

"Mageplaza_HelloWorld": {
    "name": "Mageplaza_HelloWorld",
    "setup_version": "1.0.0",
    "schema_version": "1.0.0",
    "sequence": 4,
    "module_code_pool": "community",
    "dependencies": [],
    "data": []
}

2. Incorrect UpgradeData Class Location

Ensure your UpgradeData.php class is located in the correct path within your module directory:

app/code/Mageplaza/HelloWorld/etc/Setup

The Setup folder should be named exactly as shown above, and it must exist for Magento to recognize your upgrade scripts.

3. Missing Database Schema Updates

While you have defined an UpgradeData class, which handles data-specific operations after the schema is installed, you might need an UpgradeSchema.php class to define the database schema changes required for this new attribute.

Create a new file named UpgradeSchema.php:

namespace Mageplaza\HelloWorld\Setup;

use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\SetupInterface;
use Magento\Framework\Setup\ModuleContextInterface;

class UpgradeSchema implements \Magento\Framework\Setup\UpgradeSchemaInterface
{
    private $setupFactory;

    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->setupFactory = $eavSetupFactory;
    }

    public function upgrade(SetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        if (version_compare($context->getVersion(), '1.0.0') < 0) {
            $setup->getConnection()->addColumn(
                $setup->getTable('catalog_eav_attribute'),
                'sample_attribute',
                [
                    'type' => \Magento\Framework\DB\Ddl\Table::TYPE_TEXT,
                    'length' => 255,
                    'comment' => 'Sample Attribute'
                ]
            );
        }

        $setup->endSetup();
    }
}

4. Incorrect Upgrade Sequence

Ensure your module has an appropriate sequence value in the config.xml file to ensure it is installed after other necessary core and third-party modules.

<sequence>
   <arg>50</arg>
</sequence>

Conclusion

To successfully add custom attributes to products programmatically, you need to follow these steps:

  1. Ensure your module is properly registered in the config.php file.
  2. Create an UpgradeSchema.php class in the correct path within your module directory.
  3. Define the database schema changes required for the new attribute in the UpgradeSchema.php class.
  4. Update the sequence value in your module’s config.xml to ensure it is installed after other necessary modules.

After making these adjustments, rerun the php magento setup:upgrade command. This should successfully add the ‘sample_attribute’ to your catalog product entity.

For more advanced customizations and best practices related to Magento development, consider exploring our comprehensive guides on Magento extension development.

Remember that working with Magento’s EAV system requires a good understanding of its architecture. Always test changes thoroughly in a staging environment before deploying them to production.

Happy coding!

Magento 2 Catalog Extensions Configuration

Our network