{"id":518,"date":"2019-07-22T21:12:59","date_gmt":"2019-07-22T21:12:59","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2019\/07\/22\/create-multi-select-product-attribute-in-magento-2\/"},"modified":"2025-07-21T16:31:13","modified_gmt":"2025-07-21T11:01:13","slug":"create-multi-select-product-attribute-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/create-multi-select-product-attribute-in-magento-2\/","title":{"rendered":"How to Programmatically Create Multi Select Product Attribute in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">I have shared the tutorial to&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/create-product-attribute-in-magento-2\/\" target=\"_blank\" rel=\"noreferrer noopener\">create product attribute in Magento 2<\/a>&nbsp;earlier. However, developers may need to&nbsp;create multi-select product attributes in Magento 2&nbsp;development tasks.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There is no limit when it comes to how you use this solution as per your business or client requirements. Like we use to&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/get-week-days-list-in-magento-2-system-configuration\/\">Magento 2 week days list<\/a>&nbsp;system configuration for the admin to select the days of the week to book appointment or offer delivery, etc.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The programmatic method to do so makes the task easier and quicker.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Programmatically Create Multi Select Product Attribute in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create&nbsp;<strong>InstallData.php<\/strong>&nbsp;file at&nbsp;<em><strong>Vendor\\Extension\\Setup\\&nbsp;<\/strong><\/em>folder<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Extension\\Setup;\n\nuse Magento\\Eav\\Setup\\EavSetupFactory;\nuse Magento\\Framework\\Setup\\InstallDataInterface;\nuse Magento\\Framework\\Setup\\ModuleContextInterface;\nuse Magento\\Framework\\Setup\\ModuleDataSetupInterface;\n\nclass InstallData implements InstallDataInterface\n{\n    private $eavSetupFactory;\n\n    public function __construct(EavSetupFactory $eavSetupFactory)\n    {\n        $this->eavSetupFactory = $eavSetupFactory;\n    }\n\n    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)\n    {\n        $setup->startSetup();\n        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);\n\n        \n\n        $eavSetup->addAttribute(\n            \\Magento\\Catalog\\Model\\Product::ENTITY,\n            'eway_option',\n            [\n                'group' => 'Groupe Name',\n                'label' => 'Multiselect Attribute',\n                'type'  => 'text',\n                'input' => 'multiselect',\n                'source' => 'Vendor\\Extension\\Model\\Config\\Product\\Extensionoption',\n                'required' => false,\n                'sort_order' => 30,\n                'global' => \\Magento\\Catalog\\Model\\ResourceModel\\Eav\\Attribute::SCOPE_STORE,\n                'used_in_product_listing' => true,\n                'backend' => 'Magento\\Eav\\Model\\Entity\\Attribute\\Backend\\ArrayBackend',\n                'visible_on_front' => false\n            ]\n        );\n\n        \n        $setup->endSetup();\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create&nbsp;<strong>Extensionoption.php<\/strong>&nbsp;file at&nbsp;<em><strong>Vendor\\Extension\\Model\\Config\\Product<\/strong><\/em>&nbsp;folder<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Extension\\Model\\Config\\Product;\n\nuse Magento\\Eav\\Model\\Entity\\Attribute\\Source\\AbstractSource;\n\n\nclass Extensionoption extends AbstractSource\n{\n    protected $optionFactory;\n    public function getAllOptions()\n    {\n        $this->_options = [];\n        $this->_options[] = ['label' => 'Label 1', 'value' => 'value 1'];\n        $this->_options[] = ['label' => 'Label 2', 'value' => 'value 2'];\n    \n        return $this->_options;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">With the above code you can create multi select product attributes as shown below:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2019\/07\/Screen-Recording-2019-07-17-at-11.13.56.94-AM.gif\" alt=\"Ship to specific countries\" class=\"wp-image-54271\"\/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">I\u2019d be very grateful if you helped share this helpful post on social media to fellow developers!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thanks!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have shared the tutorial to&nbsp;create product attribute in Magento 2&nbsp;earlier. However, developers may need to&nbsp;create multi-select product attributes in Magento 2&nbsp;development tasks. Developing multi&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-518","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/518","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=518"}],"version-history":[{"count":5,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/518\/revisions"}],"predecessor-version":[{"id":18798,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/518\/revisions\/18798"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=518"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=518"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=518"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}