Magento – Multi Select Product Attribute – Magento2.1.2

magento-2.1.2product-attribute

I have created Multi Select Attribute in my custom module.

UpgradeSchema.php

<?php

namespace Vendor\Module\Setup;

use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class UpgradeSchema implements UpgradeSchemaInterface
{
    protected $eavSetupFactory;

    public function __construct(
        EavSetupFactory $eavSetupFactory
    ) {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        if (version_compare($context->getVersion(), '1.0.1', '<')) {
            $eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY,'product_tags');
            $eavSetup->addAttribute(
                \Magento\Catalog\Model\Product::ENTITY,
                'product_tags',/* Custom Attribute Code */
                [
                    'group' => 'Product Details',/* Group name in which you want 
                                                  to display your custom attribute */
                    'type' => 'text',/* Data type in which formate your value save in database*/
                    'backend' => '',
                    'frontend' => '',
                    'label' => 'Your Custom Attribute Label', /* lablel of your attribute*/
                    'input' => 'multiselect',
                    'class' => '',
                    'source' => 'Vendor/Module/Model/Config/Source/Options',
                                    /* Source of your select type custom attribute options*/
                    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                                        /*Scope of your attribute */
                    'visible' => false,
                    'required' => trfalseue,
                    'user_defined' => false,
                    'default' => '',
                    'searchable' => false,
                    'filterable' => false,
                    'comparable' => false,
                    'visible_on_front' => false,
                    'used_in_product_listing' => false,
                    'unique' => false
                ]
            );
        }
    }
}

Options.php

<?php         
namespace Vendor\Module\Model\Config\Source;

class Options extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{      
    public function getAllOptions()
    {
        $this->_options=[ 
            ['label'=>'Select Options', 'value'=>''],
            ['label'=>'Option1', 'value'=>'1']
            ['label'=>'Option2', 'value'=>'2']
            ['label'=>'Option3', 'value'=>'3']
         ];
         return $this->_options;
      }
}

Can anyone please tell me what i am doing wrong here. Any help would be appreciated.

Best Answer

Please try with below UpgradeSchema

<?php

namespace Hhmedia\Tags\Setup;

use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\UpgradeSchemaInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class UpgradeSchema implements UpgradeSchemaInterface
{
    protected $eavSetupFactory;

    public function __construct(
        EavSetupFactory $eavSetupFactory
    ) {
        $this->eavSetupFactory = $eavSetupFactory;
    }

    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        if (version_compare($context->getVersion(), '1.0.1', '<')) {
            $eavSetup->removeAttribute(\Magento\Catalog\Model\Product::ENTITY,'product_tags');
            $eavSetup->addAttribute(
                \Magento\Catalog\Model\Product::ENTITY,
                'product_tags',/* Custom Attribute Code */
                [
                    'group' => 'Product Details',/* Group name in which you want 
                                                  to display your custom attribute */
                    'type' => 'text',/* Data type in which formate your value save in database*/
                    'backend' => '',
                    'frontend' => '',
                    'label' => 'Your Custom Attribute Label', /* lablel of your attribute*/
                    'input' => 'multiselect',
                    'class' => '',
                    'source' => 'Hhmedia\Tags\Model\Config\Source\Options',
                    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
                                        /*Scope of your attribute */
                    'visible' => false,
                    'required' => false,
                    'user_defined' => false,
                    'default' => '',
                    'searchable' => false,
                    'filterable' => false,
                    'comparable' => false,
                    'visible_on_front' => false,
                    'used_in_product_listing' => false,
                    'unique' => false
                ]
            );
        }
    }
}