Magento 2 Create Product Attribute Programmatically

If you want to create custom product attribute, you can use the following code snippet:
Step 1: Create the module registration file as registration.php.

Location: app/code/Magemeta/Attribute/registration.php

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

Location: app/code/Magemeta/Attribute/etc/module.xml

<?xml version="1.0" ?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Magemeta_Attribute" setup_version="1.0.0"></module>
</config>

Location: app/code/Magemeta/Attribute/composer.json

{
    "name": "magemeta/module-attribute",
    "description": "This module is used to create custom product attribute",
    "type": "magento2-module",
    "license": "OSL-3.0",
    "authors": [
        {
            "email": "info@magemeta.com",
            "name": "magemeta"
        }
    ],
    "minimum-stability": "dev",
    "require": {},
    "autoload": {
        "files": [
            "registration.php"
        ],
        "psr-4": {
            "Magemeta\\Attribute\\": ""
        }
    }
}

Step 2: Now we need to create the installer file as InstallData.php in the following location:

Location: app/code/Magemeta/Attribute/Setup/InstallData.php

<?php
namespace Magemeta\Attribute\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;

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]); 
        $eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
           'test_attribute',
           [ 
               'type' => 'text',
               'backend' => '',
               'frontend' => '',
               'label' => 'Test Attribute',
               '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' => ''
           ]
       );
    }
} ?>

If you want to delete/remove the custom product attribute, you can use the method as removeAttribute() instead of addAttribute(). Please find the below code snippet:

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context){
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
    $eavSetup->removeAttribute( \Magento\Catalog\Model\Product::ENTITY, 'test_attribute'); 
}

Leave a Reply