How to Add EAV Attribute in Magento 2 programmatically

How to Add 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

/**
     * @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' => ''
                ]
            );

 

If you are looking for Magento Developers, visit Magento Web Agency.