How to create product attribute in Magento 2.x using InstallData.php

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.

Go to the Magento app/code directory and create a directory with your vendor name. My vendor name is “CoolCodders”.

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/CoolCodders/ProductAttribute”

Step2. Create module registration file

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

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


Step3. Create module config file

<?xml version="1.0"?>
<!-- file path: MAGENTO_ROOT/app/code/CoolCodders/ProductAttribute/etc/module.xml -->

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

Step4. Create InstallData.php for creating product attributes when the module get installed.

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

namespace CoolCodders\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

php bin/magento module:enable CoolCodders_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

Published at:

Leave a comment

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