Here you’d find the steps on adding the EAV attribute for the product in Magento 2 in few options.
GET A EXTENSIVE SOLUTION WITH OUR MAGENTO WEB DEVELOPMENT SERVICES, CLICK HERE TO GET STARTED!!!
This function belongs to the Boolean expression that you can on or off. The following steps show you how to add the EAV attribute for the product in Magento 2 platform.
How to add the EAV attribute for the product in Magento?
The Given below are the examples of the Upgrade Data.php file:
- Declare EAV setup factory
- Add an EAV attribute
- Remove an 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 Product attribute in Magento
To add EAV attribute for a product by using 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' => '' ] );
Here is the following code description:
- Is_featured: Attribute code
- Group: It is the attribute name which will show on backend
- Type: Data type which will store in database
- Globar: Scale of the attribute(store, website, global)
- Visible_on_frontend: it permits the attribute to show on frontend or no
- Apply to: Product type that you need to include attribute
Remove EAV attribute to the product
The following code is used to eliminate an attribute to the product:
$entityTypeId = 4; // Find these in the eav_entity_type table $eavSetup->removeAttribute($entityTypeId, 'is_featured');
I hope, these above-mentioned steps assist you to add the EAV attribute to the product in Magento 2.
Leave a Reply