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.


<?php
['code' => 'code_of_the_attribute',
'attribute_set' => '*',
'label' => 'Concern',
'attribute_group' => 'Extra Content',
'type' => 'text',
'input' => 'multiselect',
'option' => ['values' => [
'Option 1',
'Option 2',
'Option 3',
],
],
'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
'unit' => '',
'required' => false,
'visible' => true,
'visible_on_front' => false,
'sort_order' => 2,
'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
'searchable' => true,
'filterable' => true,
'used_for_promo_rule' => true,
'comparable' => false,
'wysiwyg_enabled' => false,
'used_in_product_listing' => false,
'used_for_sort_by' => false, ]

Categories: Development

1 Comment

Pawan Meshram · January 23, 2018 at 12:33 pm

Hello,

How to save the created attribute.

I am trying to save but not working. I am using magento2.2.

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.