[Magento 2] Create Multiselect Product Attribute Programmatically

It might be tricky if you want to create a multiselect product attribute in Magento 2. For that, it’s necessary to pass the correct backend model: if you don’t do it your attribute will be created but the values won’t be saved. Taking this Inchoo article as a basis, you just need to know the fields you should pass to your attribute.

The 3 important values here are:

  • Type and Input: these 2 values have to go together for a multiselect field. The type ‘text’ is because the attribute value in bd can’t be an int, as it should be for an attribute of the type ‘select’
  • Available values for the multiselect. We added them here directly in the function, but could have created your own model or retrieve an existing one.
    'option' => ['values' => [
                'Option 1',
                'Option 2',
                'Option 3',
                ],
            ],
  • Backend: Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend. This field is very important, as it handles the saving process of the attribute. If you don’t specify it, your attribute will be displayed in the product page edition in BO, but it won’t be saved.

Leave a Reply

Your email address will not be published.