I have shared the tutorial to create product attribute in Magento 2 earlier. However, developers may need to create multi-select product attributes in Magento 2 development tasks.
Developing multi select product attributes may be required a number of times. For example, you want to develop an extension to allow the selection of more than one customer groups, locations, or select multiple options to apply a rule on.
There is no limit when it comes to how you use this solution as per your business or client requirements.
The programmatic method to do so makes the task easier and quicker.
Method to Programmatically Create Multi Select Product Attribute in Magento 2:
- Create InstallData.php file at Vendor\Extension\Setup\ folder
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647<?phpnamespace Vendor\Extension\Setup;use Magento\Eav\Setup\EavSetupFactory;use Magento\Framework\Setup\InstallDataInterface;use Magento\Framework\Setup\ModuleContextInterface;use Magento\Framework\Setup\ModuleDataSetupInterface;class InstallData implements InstallDataInterface{private $eavSetupFactory;public function __construct(EavSetupFactory $eavSetupFactory){$this->eavSetupFactory = $eavSetupFactory;}public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context){$setup->startSetup();$eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);$eavSetup->addAttribute(\Magento\Catalog\Model\Product::ENTITY,'eway_option',['group' => 'Groupe Name','label' => 'Multiselect Attribute','type' => 'text','input' => 'multiselect','source' => 'Vendor\Extension\Model\Config\Product\Extensionoption','required' => false,'sort_order' => 30,'global' => \Magento\Catalog\Model\ResourceModel\Eav\Attribute::SCOPE_STORE,'used_in_product_listing' => true,'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend','visible_on_front' => false]);$setup->endSetup();}} - Create Extensionoption.php file at Vendor\Extension\Model\Config\Product folder12345678910111213141516171819<?phpnamespace Vendor\Extension\Model\Config\Product;use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;class Extensionoption extends AbstractSource{protected $optionFactory;public function getAllOptions(){$this->_options = [];$this->_options[] = ['label' => 'Label 1', 'value' => 'value 1'];$this->_options[] = ['label' => 'Label 2', 'value' => 'value 2'];return $this->_options;}}
With the above code you can create multi select product attributes as shown below:
Do let me know in the Comments section below if you have doubts on the topic. I’ll be happy to help.
I’d be very grateful if you helped share this helpful post on social media to fellow developers!
Thanks!
Get Weekly Updates
Never miss Magento tips, tricks, tutorials, and news.
Thank you for subscribing.
Something went wrong.
4 comments On How to Programmatically Create Multi Select Product Attribute in Magento 2
Yes this module works
I’m glad it was helpful to you.
hi ..this article helped me a lot. But i have a question: if i have to set many “dropdowns” for example: a select for “age” and another one for “sizes” , do i add them in the same InstallData.php or do i have to create different instances for each select. ?
Can you help me with this issue?
thanks 🙂
Hello,
The code to create an attribute comes in the Installdata.php file. But for this option, you need to create a different file.
Thank you.