How to Add EAV Attribute in Magento 2

Magento supports two types of attributes including EAV attributes and extension attributes that provide extra functionalities for your stores. In this post, you will get an overview about EAV attributes and detailed instruction in how to add EAV attribute in Magento 2.

Overview of Magento 2 EAV attribute

To EAV (Entity Attribute Value) in Magento 2 is the core system when your business directly works with Magento 2 platform. Using EAV system allows auto-extending your models instead of there is an intervention into the database. In this topic, you will get an overview and clear instructions to set EAV attribute in Magento 2.

Your Models will still extend \Magento\Framework\Model\AbstractModel.

Your Resource Models will extend \Magento\Eav\Model\Entity\AbstractEntity which implements the EntityInterface and DefaultAttributesProvider interfaces, extra methods compared to the standard \Magento\Framework\Model\ResourceModel\Db\AbstractDb are:

  • getAttribute('code') - get an attribute from the entity

  • saveAttribute($this, 'code') - Save an attribute on an entity without going through the whole entities process

In the command to ask for your connections, you will use the __construct() method instead of the __init() method.

public function __construct(
        \Magento\Eav\Model\Entity\Context $context,
        $data = []
    ) {
        parent::__construct($context, $data);
        $this->setType('entity_type_code');
        $this->setConnection('entity_type_code_read', 'entity_type_code_write');
    }

In addition, applying the Magento\Eav\Model\Config class if you want to achieve the information related to the EAV attributes. The following methods will be considered to use:

  • getAttribute('entity_type_code', 'attribute_code')

  • getEntityType('entity_type_code')

Setting EAV attribute for product Magento 2

Create eavsetup factory

Run the following script and complete the creating EavSetup factory

/**
     * @var EavSetupFactory
     */
    protected $eavSetupFactory;
 
    /**
     * UpgradeData constructor
     *
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    } 

Add EAV attribute

/** @var EavSetup $eavSetup */
            $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
            /**
            * Add attributes to the eav/attribute
            */
            
            $eavSetup->addAttribute(
                \Magento\Catalog\Model\Product::ENTITY,
                'is_featured',
                [
                    'group' => 'General',
                    'type' => 'int',
                    'backend' => '',
                    'frontend' => '',
                    'label' => 'Is Featured',
                    'input' => 'boolean',
                    'class' => '',
                    'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                    'visible' => true,
                    'required' => false,
                    'user_defined' => false,
                    'default' => '1',
                    'searchable' => false,
                    'filterable' => false,
                    'comparable' => false,
                    'visible_on_front' => false,
                    'used_in_product_listing' => false,
                    'unique' => false,
                    'apply_to' => ''
                ]
            );

Some characters need to be followed:

  • is_featured: enter attribute code
  • group: declare the group’s name in the backend
  • type: set type of the data
  • globar: define the scope of attribute (store, website or global)
  • visible_on_frontend: select true or false option to show the attribute on the frontend or not
  • apply_to: assign to the exact product type that you want to set attribute

Remove an eav attribute for product

You can also delete the eav attribute from product by running the command:

$entityTypeId = 4; // Find these in the eav_entity_type table
            $eavSetup->removeAttribute($entityTypeId, 'is_featured');

Related Post:

Wrap up

The topic is the basic and simple knowledge you need to know about the Entity Attribute Value (EAV) in Magento 2. Though this easy-to-follow tutorial will be useful if you want to add EAV attributes to products in your Magento 2 store. If you still need any assistance about that, please comment below.

Image Description
With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.

People also searched for

  • magento 2 EAV
  • magento 2 EAV attribute
  • magento 2 EAV tutorial
  • magento 2 EAV system
  • Add EAV attribute for product
  • magento 2 create attribute programmatically
  • magento 2 extension attributes
  • magento 2 add attribute
  • magento 2 add customer attribute
  • magento 2 product attribute programmatically
  • magento 2 add customer attribute programmatically
  • add eav attribute magento 2
  • magento 2 add new eav attribute
  • magento 2 add custom eav attribute
  • magento 2 add attribute to eav
  • 2.3.x, 2.4.x
x