0

I created an issue for this in github, because i assumed this was a bug in the newest version of magento. https://github.com/magento/magento2/issues/5445

The selected value is saved the first time in the db table catalog_product_entity_varchar, and the attribute is added to the eav_attribute table, but when i load the edit product view the data does not load, i.e. the selected options are not highlighted against the attribute. enter image description here

Attribute installation is as follows $this->addMultiSelectFieldWithSource($eavSetup, 'special_requirements', 'Special requirements', false, 4,

'Amrita\Catalog\Model\Product\Attribute\Source\SpecialRequirements');   

 private function addMultiSelectFieldWithSource($eavSetup, $fieldName, $fieldTitle, $required, $position, $source) {
    $eavSetup->addAttribute(
        Product::ENTITY,
        $fieldName,
        [
            'type' => 'varchar',
            'label' => $fieldTitle,
            'input' => 'multiselect',
            'required' => $required,
            'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
            'source' => $source,
            'global' => Attribute::SCOPE_GLOBAL,
            'visible' => true,
            'user_defined' => true,
            'system' => 0,
            'searchable' => true,
            'visible_in_advanced_search' => true,
            'filterable' => true,
            'filterable_in_search' => true,
            'position' => $position,
            'comparable' => false,
            'visible_on_front' => false,
            'unique' => false,
            'group' => 'General',
            'is_used_in_grid' => true,
            'is_visible_in_grid' => false,
            'is_filterable_in_grid' => true,
        ]
    );

And the source for the attribute is as follows

    <?php

namespace Amrita\Catalog\Model\Product\Attribute\Source;
use Magento\Eav\Model\Entity\Attribute\Source\AbstractSource;

class SpecialRequirements extends AbstractSource
{
    /**
     * @return array
     */
    public function getAllOptions()
    {
        return [
            ['value' => 1,               'label' => 'Suitable for Vegetarians'],
            ['value' => 2,  'label' => 'Suitable for Pregnancy or Breastfeeding'],
            ['value' => 3,                    'label' => 'Suitable for Vegans'],
            ['value' => 4,                 'label' => 'Suitable for Children']
        ];
    }
}

Is this to do with the upgrade? Or is this an issue with the installation of the attribute?

Thanks

  • Can you find any solution? if yes than post it as answer. – ND17 Aug 20 '16 at 11:23
1

It looks like this can be fixed by having string values instead of integers in the source. So ['value' => 1, ...] becomes ['value' => '1'].

I'm uncertain why this is the case but it seems to have something to do with the way KnockoutJS works out which values to select.

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.