Add Multiple Product Attribute Using InstallData in Magento2

In this article, I am going to explain how to add multiple product attribute or create product attributes by using the installData.php file programmatically in Magento 2.

Magento 2  eCommerce follows the EAV database model, So in such a situation, it becomes necessary that its standard should be followed. So in order to add the custom attributes in Magento 2, we have to create the InsatllData PHP file.

In the chances of any online e-commerce website being successful, it matters a lot that how well you define the product.

When we want to define the product in a better way, we take the help of custom attribute which plays an important role in conveying to the customer of that store easily.

Custom product attribute is one of the important characteristics of the products. That is responsible to define the character and specification of the product so the customer easily understands their requirement and influence to buy that product. Examples of product attributes can be any type like product price,  product quantity, product size, etc.

 

Steps to Add Product Attribute Programmatically

In order to create the product attributes we have to follow the following steps, Wishusucess MultipleAttribute module will help you to understand how to add a product attribute in Magento 2.

  • Create file InstallData.php
  • Define the install() method
  • Create custom attribute

 

In the first step, we have to create an InstallData file then in the second step we will define the install method and in the third step, we will add a custom attribute.

 

Magento 2 Add Multiple Product Attribute Programmatically

We will create a file inside of Setup folder with the InstallData file name in the below location.

Wishusucess/ProductAttributes/Setup/InstallData.php

Now the content will look like the below class.

<?php
namespace Wishusucess\ProductAttributes\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
{
/**
* EAV setup factory
*
* @var EavSetupFactory
*/
private $eavSetupFactory;

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

public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/** @var EavSetup $eavSetup */
$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);

/**
* Add attributes to the eav/attribute
*/
$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'wishusucess_featured',
[
'group' => 'General',
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'Featured Product',
'input' => 'boolean',
'class' => '',
'source' => '',
'global' => \Magento\Catalog\Model\ResourceModel\Eav\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'
],
);

$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'wishusucess_bestseller',
[
'group' => 'General',
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'Bestseller Product',
'input' => 'boolean',
'class' => '',
'source' => '',
'global' => \Magento\Catalog\Model\ResourceModel\Eav\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'
],
);

$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY,
'wishusucess_specialproducts',
[
'group' => 'General',
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'Special Product',
'input' => 'boolean',
'class' => '',
'source' => '',
'global' => \Magento\Catalog\Model\ResourceModel\Eav\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'
],
);

$eavSetup->addAttribute(
\Magento\Catalog\Model\Product::ENTITY, 
'wishusucess_newproducts',
[
'group' => 'General',
'type' => 'int',
'backend' => '',
'frontend' => '',
'label' => 'New Product',
'input' => 'boolean',
'class' => '',
'source' => '',
'global' => \Magento\Catalog\Model\ResourceModel\Eav\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'
]
);
}
}

 

Step 4: Run Magento 2 Command

Now run the below command

php bin/magento setup:upgrade

php bin/magento setup:static-content:deploy -f

php bin/magento c:c

That's it!!

Now go to the Admin > Catalog > Product > Click on any product you will see like the below image.

Add multiple product attribute in Magento 2

The above code will help you to create multiple filter product attribute like best seller attribute, featured attribute, a new attribute, special attribute by using a single file.

 

If you are looking for this complete extension then you can check our GitHub link.

 

Related article

How to Get Data on Frontend From System Configuration File in Magento2

Add JS File in Magento 2 Module: How to Add Java Script