Magento 2: Add Product Attribute Programmatically

Magento 2: Add Product Attribute Programmatically

Magento 2 – Add Product Attribute Programmatically

In eCommerce, a trivial thing such as adding product attributes can help you attract and retail customers. How? By adding relevant and detailed product attributes, you can communicate vital information to the audience. Suppose you are an online gym apparel store in Dubai.

A search for ‘gym clothes Dubai’ leads to 8,620,000 results as you can see below.

Source: Google Search

A potential customer does not have the time to go through all these results and then compare the products. Instead, they will open first 6 links and assess whether the products meet their requirement. How can you ensure that your store coverts this potential lead to a customer?

By adding attributes such as quality and fabric type, you are providing additional information. It makes it easier to decide if the product is according to their needs. For example, the individual may be looking for cotton gym trousers. All five store may be offering cotton trousers, but this is not mentioned anywhere.

In today’s fast-paced environment, the customer will not call or message the store to confirm the fabric type. When your store is the only one mentioning the fabric type, the customer will naturally prefer to shop at your store. This is how the effective use of product attributes can come in handy.

Check Out:

Before we talk about product attributes in Magento 2, let’s first develop a comprehensive understanding of product attributes.

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, colour, quality, etc. Adding a new & important product attribute is one of the most popular operations in Magento.

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. Why programmatically when Magento already has a user-friendly way to add attributes?

Why Add Product Attribute Programmatically Magento 2?

In some cases, Magento’s product attribute function is not effective. This is the case when you have to create bulk attributes. Adding them programmatically saves time and effort compared to the admin panel. Similarly, when using external data sources for the attributes, adding them programmatically is recommended.

Steps to Create Product Attribute in Magento 2 Programmatically

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’.

Conclusion

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.