Today I am going to explain, how we can create drop down attribute for Products in Magento 2.
There are two ways to create the drop down attribute in Magento.
1 -> Manually : Go To admin panel, Store -> Attributes -> Product .
2 -> Programmatically .
In this blog I am covering how we can create the drop down attribute programmatically,
So let’s start :
Step 1 : create InstallData.php file : Company\Module\Setup\InstallData.php
Now In this file, create an Array of the attributes that you want to add as below.
$attributeGroup = 'Test Group'; $attributes = [ 'attribute_code' => [ 'group' => $attributeGroup, 'input' => 'select', 'type' => 'int', 'label' => 'Attribute Label', 'visible' => true, 'required' => false, 'user_defined' => true, 'searchable' => false, 'filterable' => false, 'comparable' => false, 'visible_on_front' => false, 'visible_in_advanced_search' => false, 'is_html_allowed_on_front' => false, 'used_for_promo_rules' => true, 'source' => \Company\Module\Model\Config\Source\Options::class, 'frontend_class' => '', 'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL, 'unique' => false, 'apply_to' => 'simple,grouped,configurable,downloadable,virtual,bundle' ],... ];
Step 2 : Now Add and Assign Attribute to Group and Attribute Set.
// Add Attribute // Magento\Eav\Setup\EavSetupFactory $this->eavSetupFactory // Magento\Framework\Setup\ModuleDataSetupInterface $setup $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]); foreach ($attributes as $attribute_code => $attributeOptions) { $eavSetup->addAttribute( \Magento\Catalog\Model\Product::ENTITY, $attribute_code, $attributeOptions ); } //Assign foreach ($attributes as $attribute_code => $attributeOptions) { // Class \Magento\Eav\Model\AttributeManagement $this->attributeManagement->assign( 'catalog_product', $attributeSet->getId(), $groupId, $attribute_code, $attributeSet->getCollection()->getSize() * 10 ); }
Note :
$attributeSet : you can load the attribute collection and loop for it or Can pass specific id instead.
$groupId : Magento\Eav\Model\Entity\Attribute\Group : you can get the group id with the help of this class.
Now , Options for drop down attribute , you can add options directly as a array.
and another approach is to use class Magento\Eav\Model\Entity\Attribute\Source\AbstractSource as follows :
<?php namespace Company\Module\Model\Config\Source; class Options extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource { /** * Get all options * * @return array */ public function getAllOptions() { $this->_options = [ ['label' => __('No'), 'value'=>'0'], ['label' => __('Yes'), 'value'=>'1'], ['label' => __('Other'), 'value'=>'2'] ]; return $this->_options; } }
Please read each line carefully, so that you can create the attributes easily.
and In case you have any Query then feel free to ask in comment section below.
2 comments
If there is any thing else with which you are confused, that I have missed to explain so please explain in more details.
In second step, we have just added and assigned the attribute to the attribute set and group.