After looking for a proper solution on how to create programmatically a multiselect customer attribute along with its entensible options, I haven’t found anything that was good for me:

  • In many places it is proposed to create a custom class with the options. That’s not good for me because I want the options to be extensible using a cool module like this (it allows creating customer attributes from backoffice).
  • Other solutions I found that should have been enough proposed to use a backend model that would break everything (Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend).

Well, the final solution that has worked is this one:

$customerSetup->addAttribute(
    Customer::ENTITY,
    'customer_segment',
    [
        'label' => 'Segment',
        'input' => 'multiselect',
        'type' => 'text',
        'source' => 'Magento\Eav\Model\Entity\Attribute\Source\Table',
        'required' => false,
        'position' => 666,
        'visible' => true,
        'system' => false,
        'is_used_in_grid' => true,
        'is_visible_in_grid' => true,
        'is_filterable_in_grid' => true,
        'is_searchable_in_grid' => true,
        'backend' => '',
        'option' => ['value' =>
            [
                'option_1'=>[
                    0 => 'Test 1'
                ],
                'option_2'=>[
                    0 => 'Test 2'
                ],
            ],
            'order'=>
                [
                    'option_1' => 1,
                    'option_2' => 2
                ]
        ],
    ]
);
Categories: Uncategorized

0 Comments

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.