how to add EAV attribute for product in Magento 2

It is a Boolean function that you can on or off. The steps describes you how to add EAV attribute for product in Magento 2.

Examples of Upgrade Data.php file listed:

  1. Declare EAV setup factory
  2. Add EAV attribute
  3. Remove EAV attribute for product

Declare EAV setup factory

  /**

     * @var EavSetupFactory

     */

    protected $eavSetupFactory;

    /**

     * UpgradeData constructor

     *

     * @param EavSetupFactory $eavSetupFactory

     */

    public function __construct(EavSetupFactory $eavSetupFactory)

    {

        $this->eavSetupFactory = $eavSetupFactory;

    }

Add EAV attribute

To add EAV attribute for product the following code:

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

                ]

            );

The following code description:

  • Is_featured: attribute code
  • Group: name for attribute that will display on backend
  • Type: data type which will save in database
  • Globar: scale of attribute(store,website,global)
  • Visible_on_frontend: it allows the attribute to displayed on frontend or no
  • Apply to:product type that you want to add attribute

Remove attribute to the product

The following code to remove attribute to the product:

$entityTypeId = 4; // Find these in the eav_entity_type table

            $eavSetup->removeAttribute($entityTypeId, 'is_featured');

The above mentioned steps help you to add EAV attribute to the product in magento 2

Last Update: April 9, 2018  

February 6, 2018   215   Nandini R    Products    
Total 1 Votes:
0

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Reply

Your email address will not be published. Required fields are marked *

Facebook
Google+
http://www.webnexs.com/blog/kb/add-eav-attribute-product-magento-2-2
Twitter
LinkedIn