Create Product Attribute Of Type MULTISELECT or dropdown select & Create Option Labels Diffrent for Diffrent Store using install or upgrade schema

magento.stackexchange https://magento.stackexchange.com/questions/306710

Question

I am creating custom Product Attribute Of type MULTI SELECT,

I want to create different option label for each stores for this particular Attribute,

I have tried with this below code,

$eavSetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'myattribute',
            [
                'type' => 'text',
                'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                'frontend' => '',
                'label' => 'My Attribute',
                'input' => 'multiselect',
                'class' => '',
                'option' => ['values' => 
                                [
                                    '0' => '1',
                                    '1' => '2',
                                    '2' => '3',
                                    '3' => '4',
                                    '4' => '5',
                                    '5' => '6',
                                ],
                            ],  // I have tried this way but not working
                'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                'visible' => true,
                'required' => false,
                'user_defined' => true,
                'default' => '',
                'searchable' => false,
                'filterable' => false,
                'comparable' => false,
                'visible_on_front' => false,
                'used_in_product_listing' => true,
                'wysiwyg_enabled' => true,
                'unique' => false,
                'apply_to' => ''
            ]
        );

With the above code, Attribute Options were inserted as a new row & I want it to be stored as store wise.

How can I achieve this thing.?

Was it helpful?

Solution

I have check the core how magento install the label and I found code in below file

vendor/magento/module-eav/Setup/EavSetup.php

in this file I got addAttributeOption method.

After seeing the code I got your answer, create attribute like below

You can create multiselect and dropdown attribute option label different on store basis like below

$eavSetup->addAttribute(
        \Magento\Catalog\Model\Product::ENTITY,
        'myattribute',
        [
            'type' => 'text',
            'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
            'frontend' => '',
            'label' => 'My Attribute',
            'input' => 'multiselect',
            'class' => '',
    // instead values change your key as value and add your option like below with in `option_1`  array put key as store_id and label as value 
            'option' => ['value' => 
                            [
                             'option_1'=>[ 
                            //option_1 will be static it will add below labels in option_1 for **new**
                                  0=>'label for admin',    // here 0 is store id and 123 is value
                                  1=>'label for store1',
                                  13=>'label for store 13',
                                  14=>12121,
                                  15=>1212,
                                  16=>123
                                  ],
                            'option_2'=>[
                                  0=>'label for admin',
                                  1=>'label for store1',
                                  13=>'label for store 13',
                                  14=>32123,
                                  15=>123123123,
                                  16=>5152
                                  ],
                            ],
                           'order'=>//Here We can Set Sort Order For Each Value.
                                 [
                                      'option_1'=>1,
                                      'option_2'=>2
                                 ]
                        ], 
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
            'visible' => true,
            'required' => false,
            'user_defined' => true,
            'default' => '',
            'searchable' => false,
            'filterable' => false,
            'comparable' => false,
            'visible_on_front' => false,
            'used_in_product_listing' => true,
            'wysiwyg_enabled' => true,
            'unique' => false,
            'apply_to' => ''
        ]
    );

OTHER TIPS

If you want to show your attribute on the list page in the filter.

<?php
namespace Namespace\ModuleName\Setup;

use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\UpgradeDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use function Sodium\compare;

class UpgradeData implements UpgradeDataInterface
{

    private $eavSetupFactory;

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

    public function upgrade(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        if (version_compare($context->getVersion(), '1.0.5', '<=')) {
            $eavSetup->addAttribute(
                \Magento\Catalog\Model\Product::ENTITY,
                'brand',
                [
                    'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                    'type' => 'varchar',
                    'frontend' => '',
                    'label' => 'Brand',
                    'input' => 'multiselect',
                    'class' => '',
                    'option' => '',
                    'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                    'visible' => true,
                    'required' => false,
                    'user_defined' => true,
                    'default' => '',
                    'searchable' => true,
                    'filterable' => true,
                    'comparable' => false,
                    'visible_on_front' => true,
                    'used_in_product_listing' => true,
                    'unique' => false,
                    'apply_to' => ''
                ]
            );
        }
    }
}

You have to remove module from setup_module table. Than delete its folder from app/code folder php bin/magento setup:upgrade now put module folder again and installl again with

backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
                'type' => 'varchar',
                'frontend' => '',
                'label' => 'Brand',
                'input' => 'multiselect',
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top