Create Custom Attribute Group & Product Attribute in Magento 2 via InstallData.php

In Magento 2, we can create many number of product attributes and attribute groups on the admin without any issues. But sometimes we may need to create new product attribute and attribute group through installable script. For instance, while we develop a new extension in Magento 2 or customizing the native features of the Magento 2, we have to use installable script to create new product attribute and attribute group. Creating product attribute and attribute group is not a big work and  this article will help you how to create new attribute group and product attribute in Magento 2 via InstallData.php.

Let’s assume that we are going to create a new attribute group called “My Custom Attribute Group” and product attribute called “My Sample Attribute” by using the following scripts.

Step 1: Define new module

In order to create new extension in Magento 2, first we have to create a file named module.xml in the below file path to define the new module,

File Path: app/code/MyModule/MyAttribute/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="MyModule_MyAttribute" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>

Next, create registration.php file in the below file path,

File Path: app/code/MyModule/MyAttribute/registration.php

<?php
    \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'MyModule_MyAttribute',
        __DIR__
    );

Step 2: Create setup file InstallData.php

Then, create a file named InstallData.php in the below file path. In this file, we have to provide the name of newly create product attribute group and the attribute information,

File Path: app/code/MyModule/MyAttribute/Setup/InstallData.php

<?php
namespace MyModule\MyAttribute\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\Config;
 
class InstallData implements InstallDataInterface
{
    private $eavSetupFactory;
  
    public function __construct(EavSetupFactory $eavSetupFactory, Config $eavConfig)
    {
        $this->eavSetupFactory = $eavSetupFactory;
        $this->eavConfig = $eavConfig;
    }
 
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
 
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'my_sample_attribute',
            [
                'group' => 'My Custom Attribute Group',
                'type' => 'varchar',
                'backend' => '',
                'frontend' => '',
                'label' => 'My Sample Attribute',
                'input' => 'text',
                'note' => 'My Sample Attribute',
                'class' => '',
                'source' => '',
                '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' => true,
                'used_in_product_listing' => true,
                'unique' => false,
                'apply_to' => 'simple'
            ]
        );
    }
}

Step 3: Module setup upgrade

Finally, we have to enable our new extension by running the following SSH command in the Magento installed root directory,

php magento module:enable MyModule_MyAttribute

Then, run the below command in your CLI to upgrade the setup,

php magento setup:upgrade

Then, clear the cache and check your Magento 2 admin. Now you can able to see the newly created product attribute group and product attribute on the admin side like the reference image below.

Reference image:

Create Custom Attribute Group & Product Attribute in Magento 2 via InstallData.php

Read: Create an Attribute Set in Magento 2 via Installable Script

Hope this helps.

Leave a Reply

Your email address will not be published. Required fields are marked *