Magento 2 Multi select custom product attribute options not showing

3.2k Views Asked by At

In my custom module, used installData.php to create a custom multiselect attribute. Where i have set the option values from my source class (using Magento\Eav\Model\Entity\Attribute\Source\AbstractSource) which is working fine after installation. I can see the options while editing the product.

But the options are not visible while editing the attribute. Im not able to add/remove option after this.

Please advise.

$eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'my_option',
        [
            'group' => 'General',
            'label' => 'My Label',
            'type'  => 'text',
            'input' => 'multiselect',
            'user_defined' => true,
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'source' => 'Vendor\Module\Model\Attribute\Source\Options',
            'required' => false,
            'filterable' => true,
            'filterable_in_search' => true,
            'is_searchable_in_grid' => false,                
            'is_used_in_grid' => false,
            'is_visible_in_grid' => false,
            'is_filterable_in_grid' => false, 
            'sort_order' => 200,
            'used_in_product_listing' => true,
            'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
            'visible' => true,
            'visible_on_front' => true,
            'searchable' => false,
            'comparable' => false,
        ]
    );
1

There are 1 best solutions below

0
Milan Maniya On

1. Create InstallData.php file at Vendor\Extension\Setup\ folder.

<?php

namespace 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();
    }
}

2. Create Extensionoption.php file at Vendor\Extension\Model\Config\Product folder.

<?php
 
namespace 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;
    }
}