How to create product attribute in Magento 2.x

02 Jun, 2021

Today we create a module for creating product attribute in Magento 2. This blog is for Magento 2.x product attribute. How we can create attribute using our custom module.

Let's start by creating a custom attribute module.


Step1. Create module directory

Go to Magento app/code directory and create a directory with your vendor name. My vendor name is "RS".

After creating the vendor directory, need to create a directory for the module. My module name is "ProductAttribute".

My custom module directory path is like "MAGENTO_ROOT/app/code/RS/ProductAttribute"

Step2. Create module registration file

	<?php
		//file path: MAGENTO_ROOT/app/code/RS/ProductAttribute/registration.php

		\Magento\Framework\Component\ComponentRegistrar::register(
		    \Magento\Framework\Component\ComponentRegistrar::MODULE,
		    'RS_ProductAttribute',
		    __DIR__
		);
	

Step3. Create module config file

	
	<?xml version="1.0"?>
	<!-- file path: MAGENTO_ROOT/app/code/RS/FirstModule/etx/module.xml -->

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

Step4. Creating a InstallData.php file for creating product attribute.

	
		<?php
		//file path: MAGENTO_ROOT/app/code/RS/ProductAttribute/Setup/InstallData.php

		namespace RS\ProductAttribute\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\Catalog\Model\ResourceModel\Eav\Attribute;

		class InstallData implements InstallDataInterface
		{
		    /**
		     * EAV setup factory
		     *
		     * @var EavSetupFactory
		     */
		    private $_eavSetupFactory;

		    /**
		     * Init
		     *
		     * @param EavSetupFactory $eavSetupFactory EnvSetupfactory
		     */
		    public function __construct(EavSetupFactory $eavSetupFactory)
		    {
		        $this->_eavSetupFactory = $eavSetupFactory;
		    }

		    /**
		     * Install Data script
		     *
		     * @param ModuleDataSetupInterface $setup   Setup Object
		     * @param ModuleContextInterface   $context Context Object
		     *
		     * @return void
		     */
		    public function install(
		        ModuleDataSetupInterface $setup,
		        ModuleContextInterface $context
		    ) {
		        
		        $eavSetup = $this->_eavSetupFactory->create(['setup' => $setup]);
		        $eavSetup->addAttribute(
		            \Magento\Catalog\Model\Product::ENTITY,
		            'custom_attr',
		            [
		            'group' => 'Product Details',
		            'type' => 'varchar',
		            'sort_order' => 100,
		            'label' => 'Custom Attribute 01',
		            'input' => 'text',
		            'global' => Attribute::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,virtual,bundle,downloadable'
		            ]
		        );
		    }
		}
	

Step6. Install your module to magento & Creating product attributes

	
		php bin/magento module:enable RS_ProductAttribute
		php bin/magento setup:di:compile
		php bin/magento setup:upgrade
		php bin/magento setup:static-content:deploy
	

That is the steps for creating a custom product attribute using programmatically

RAJU SADADIYA (MAGENTO DEVELOPER)