Magento 2: Add Product Attribute Programmatically

Magento 2: Add Product Attribute Programmatically

What is A Product Attribute?

A product attribute defines a characteristic of a particular product. Product attributes are visible to customers and affect their purchase decisions. Examples of product attributes include price, volume, size, color, quality, etc. Adding a new & important product attribute is one of the most popular operations in both Magento 1 & Magento 2. Product attributes are used frequently in various product related operations and thus are a powerful way to solve practical tasks. In this article, we are going to see how to add a new product attribute programmatically.

Steps to Add Product Attribute Programmatically in Magento 2

Follow these steps to create product attributes programmatically:

Step 1: Create the Setup File InstallData.php

Start by creating the setup file: File Path: app/code/Company/Mymodule/Setup/InstallData.php
<?php
namespace Company\Mymodule\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;

class InstallData implements InstallDataInterface
{
 private $eavSetupFactory;

 public function __construct(EavSetupFactory $eavSetupFactory)
 {
 $this->eavSetupFactory = $eavSetupFactory;
 }
 
}

Step 2: Define the install() Method

After creating the setup file, define the installation method. a programmable function is written below for this.
<?php
 
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
 
}

Step 3: Create Product Attribute Programmatically

As per your need, create new product attribute of any type like text field, drop-down, radio button etc. With below code, you can create a custom text field.
<?php
namespace Company\Mymodule\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;

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,
 'sample_attribute',
 [
 'type' => 'text',
 'backend' => '',
 'frontend' => '',
 'label' => 'New Product Atrribute',
 'input' => 'text',
 'class' => '',
 'source' => '',
 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
 'visible' => true,
 'required' => false,
 'user_defined' => false,
 'default' => '',
 'searchable' => false,
 'filterable' => false,
 'comparable' => false,
 'visible_on_front' => false,
 'used_in_product_listing' => true,
 'unique' => false,
 'apply_to' => ''
 ]
 );
 }
}

Step 4: Upgrade

Open terminal/SSH and navigate to Magento 2 setup root directory and run the commands below.
php bin/magento setup:upgrade
php bin/magento setup:static-content:deploy -f
This will add the new product attribute titled ‘New Product Attribute’. If you're looking to add new attributes from the product attribute page, then follow this magento user guide. If you have any issue in creating product attribute programmatically in Magento 2, then feel free to contact our support team for a quick fix.