1

I have created a custom Multiselect product attribute. It has been created successfully but the value are saved when assigned store view specific.

But if i select an option and save in global it is not saving.Anything else i am missing?

Step1: setup file InstallData.php

namespace Vendor\Module\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;


class InstallData implements InstallDataInterface
{
/**
 * @var \Magento\Eav\Setup\EavSetupFactory
 */
private $eavSetupFactory;

/**
 * Constructor
 *
 * @param \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory
 */
    public function __construct(EavSetupFactory $eavSetupFactory)
    {
        $this->eavSetupFactory = $eavSetupFactory;
    }


    public function install(ModuleDataSetupInterface $setup,  ModuleContextInterface $context)
    {

       $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
       $eavSetup->addAttribute(
           \Magento\Catalog\Model\Product::ENTITY,
           'attr_code',
           [
              'type' => 'text',
              'label' => 'Attr Label',
              'input' => 'multiselect',
              'sort_order' => 2,
              'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
              'source' => 'Vendor\Module\Model\Config\Source\Options',
              'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
              'visible' => true,
              'required' => false,
              'user_defined' => false,
              'filterable' => true,
              'used_in_product_listing' => true,
          ]
      );
   }
}

Step2: Source Model

namespace Vendor\Module\Model\Config\Source;

class Options extends \Magento\Eav\Model\Entity\Attribute\Source\AbstractSource
{

   public function getAllOptions()
   {
       $this->_options = [
            ['label' => __('Label1'), 'value'=>'0'],
            ['label' => __('Label2'), 'value'=>'1'],
        ];
        return $this->_options;
   }

   public function toOptionArray()
   {
       $arr = $this->getAllOptions();
       $_options = [];
       foreach ($arr as $key => $value) {
          $_options[] = [
              'value' => $key,
              'label' => $value
          ];
       }

      return $_options;
   }
}
0

Add your attribute to the attribute group

$setId = $eavSetup->getDefaultAttributeSetId($entityType); 
$groupId = $eavSetup->getDefaultAttributeGroupId($entityType, $setId); 
$groupName = $eavSetup->getGroupName($entityType, $setId, $groupId, 'attribute_group_name');

And also change the user_defined to true and add 'group' => $groupName

| improve this answer | |
  • This needs to be add in setup file ? – aravind May 28 '19 at 11:25
  • @aravind yes, those changes needs to be done in setup file. – Anshu Mishra May 28 '19 at 11:54
  • ok will check and update you @Anshu Mishra – aravind May 28 '19 at 11:54
0

you have not set when creating attribute

like below:

'option' => ['values' => [
            'Option 1',
            'Option 2',
            'Option 3',
            ],
        ],

for more please check below blog

| improve this answer | |
  • It is for adding option statically in setup file..But i have source model for this attribute.. – aravind May 28 '19 at 11:41

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.