Milind Daraniya

Magento 2: Creating Products Attributes Programmatically

Published September 18th, 2023 11 min read

In Magento 2, product attributes are crucial for categorizing and providing information about your products. Sometimes, you might need to create product attributes programmatically, especially when developing custom modules or performing data migrations. In this tutorial, we'll walk you through the process of creating product attributes programmatically in Magento 2.

Step 1: Create a Custom Module

First, create a custom module or use an existing one where you want to define the new product attributes.

Step 2: Define the Product Attribute Setup

Inside your custom module directory, create a new file named InstallData.php under the Setup directory. This file will handle the installation of the new product attributes.

<?php
namespace Vendor\Module\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;

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

    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

        // Create a new product attribute
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'custom_attribute_code',
            [
                'type' => 'int', // Data type of the attribute (text, int, decimal, etc.)
                'backend' => '',
                'frontend' => '',
                'label' => 'Custom Attribute Label',
                'input' => 'select', // Input type (text, select, textarea, etc.)
                'class' => '',
                'source' => '', // Source model for select-type attributes
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => true,
                'required' => false,
                'user_defined' => true,
                'default' => '',
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => 'simple,configurable', // Apply the attribute to product types (simple, configurable, etc.)
                'group' => 'General', // Attribute group in admin panel
                'sort_order' => 50,
            ]
        );
    }
}

Step 3: Run the Setup Upgrade

After defining the product attribute setup, run the setup upgrade command to apply the changes:

php bin/magento setup:upgrade

Step 4: Verify the Product Attribute

Go to your Magento admin panel and navigate to Stores > Attributes > Product. You should see your newly created product attribute listed there.