3

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.?

  • If i have a business and a personal store and want separate registration and drop down selection would i use this same method? also instead of > 'option_1'=>1,'option_2'=>2 can i illustrate as > option_personal'=>personal 'option_business'=>Business? and how would i point (depending on registration) to the correct store front end? Thank you @MurtuzaZabuawala – rav narayan Jan 4 '19 at 6:13
  • @ravnarayan it will not save – Murtuza Zabuawala Jul 12 at 4:37
7

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' => ''
        ]
    );
| improve this answer | |
  • What should i do to add file path in option attribute instead of add all options here? – Emipro Technologies Pvt. Ltd. Mar 11 '19 at 12:30
  • Hi I @murtuza zabuawala i would like to add options data to my multiselect attribute how to add prnt.sc/qwogpd can u guide? – Nagaraju K Feb 3 at 6:18
  • @NagarajuK this answer only for M2 Even I need to check for M1 how to do this – Murtuza Zabuawala Feb 3 at 6:45
  • No No, I need the answer in M2 only @MurtuzaZabuawala – Nagaraju K Feb 3 at 6:46
  • @NagarajuK so this answer for m2 multiselect only – Murtuza Zabuawala Feb 3 at 7:00
0

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' => ''
                ]
            );
        }
    }
}
| improve this answer | |
  • Not a relevant answer to this question. please check question properly. – Rizwan Khan Mar 11 at 7:43
  • @RizwanKhan yes! but from UpgradeData you can also do this same and none of the above answers is working perfectly. – Manish Goswami Mar 11 at 9:06
0

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',
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.