Magento, Magento 2

How to Create Product Attribute Programmatically in Magento 2

How to Create Product Attribute Programmatically in Magento 2

In this tutorial, Today I will explain to how to create product attribute programmatically in Magento 2. To create product attribute, we need to create InstallData.php file in your custom module. Magento 2 Product Structure follows EAV structure. So, we can not create new attribute from database table.

When, you will setup your module at that time, InstallData file will execute and create new product attribute. After that, To see that attribute in product edit form you need to set that attribute in appropriate attribute set and save that attribute set.

First of all, you should have your own Magento 2 Extension. See how to create your module here.

Let’s follow the below steps to Create Product Attribute Programmatically in Magento 2.

You may also like this :

1) Create InstallData.php file at app/code/RH/Helloworld/Setup/ and paste the below code :


<?php

/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

/**
 * Created By : Rohan Hapani
 */

namespace RH\HelloWorld\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;
use Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface;
use Magento\Catalog\Model\Product;

class InstallData implements InstallDataInterface
{
    /**
     * @var EavSetupFactory
     */
    private $eavSetupFactory;

    /**
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }
    
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        $eavSetup->addAttribute(
            Product::ENTITY,
            'product_temp_attribute',
            [
                'type' => 'text',
                'backend' => '',
                'frontend' => '',
                'label' => 'Product Temp Atrribute',
                'input' => 'text',
                'class' => '',
                'source' => '',
                'global' => ScopedAttributeInterface::SCOPE_GLOBAL,
                'visible' => true,
                'required' => true,
                'user_defined' => false,
                'default' => '',
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => ''
            ]
        );
    }
}

2) After that, you need to upgrade your module using below command :


php bin/magento s:up
php bin/magento s:s:d -f
php bin/magento c:c

That’s it !!!

You can check this custom attribute in eav_attribute table that attribute creates successfully or not.

I hope this blog is easy to understand about how to add custom customer attribute in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution.

Keep liking and sharing 🙂

Tagged ,