How to Add EAV Attribute in Magento 2

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.

Overview of Magento 2 EAV attribute

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');

The topic is the basic and simple knowledge you need to know about the Entity Attribute Value (EAV) in Magento 2. If you still need any assistance about that, please comment below.



Next tutorial:

Module Development Series



Enjoyed the tutorial? Spread it to your friends!

magento-2-tutorial
EAV-attribute
EAV-tutorial
create-EAV-attribute

Module Development

Comments for How to Add EAV Attribute in Magento 2

Please leave comments if you have any questions, feedbacks.

You also may like these Magento 2 Extensions

One Step Checkout

$199
10 reviews

Layered Navigation

$99
12 reviews

SEO

FREE
no review

Shop By Brand

$99
3 reviews

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