0

I have successfully added a multiselect attribute to the adminhtml customer edit form, but when saving the customer the selected values are not saved.

$customerSetup->addAttribute(Customer::ENTITY, 'customattribute', [
    'type' => 'text',
    'label' => 'Custom Attribute',
    'input' => 'multiselect',
    'backend' => 'Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend',
    'source' => 'My\Module\Model\Config\Source\CustomOptions',
    'required' => false,
    'visible' => true,
    'user_defined' => true,
    'sort_order' => 1000,
    'position' => 1000,
    'system' => 0,
]);

EDIT:

The source class:

class PortalCategoryOptions extends AbstractSource {
    protected $_categoryHelper;

    protected $_categoryCollectionFactory;

    public function __construct(Category $catalogCategory, CollectionFactory $categoryCollectionFactory) {
        $this->_categoryHelper = $catalogCategory;
        $this->_categoryCollectionFactory = $categoryCollectionFactory;
    }

    public function getStoreCategories($sorted = false, $asCollection = false, $toLoad = true) {
        $collection = $this->_categoryCollectionFactory->create();
        $collection->addAttributeToSelect('*');
        $collection->setOrder('name');

        return $collection;
    }

    public function getAllOptions() {
        $arr = $this->toArray();
        $ret = [];

        foreach ($arr as $key => $value)  {
            $ret[] = [
                'value' => $key,
                'label' => $value
            ];
        }

        return $ret;
    }

    public function toArray() {
        $categories = $this->getStoreCategories(true,false,true);

        $categoryList = array();
        foreach ($categories as $category){
            if ($category->getEntityId() >= 3) {
                $categoryList[$category->getEntityId()] = __($category->getName());
            }
        }

        return $categoryList;
    }
}
  • Can you show further code which you used to add attribute in customer edit form? – Dhiren Vasoya Apr 5 '17 at 5:53
  • I have added the rest of the code, but I don't think it's relevant since that part of the code works fine. – Kaascroissant Apr 5 '17 at 9:06
  • I am asking for the code, which you write to add above customer attribute in customer edit section. – Dhiren Vasoya Apr 5 '17 at 10:14
  • Can you finaly solve this? i have the same issue – Diego Sanabria Sep 18 '18 at 14:36
  • This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. You can also add a bounty to draw more attention to this question once you have enough reputation. - From Review – Rama Chandran M Sep 18 '18 at 15:00

Your Answer

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

Browse other questions tagged or ask your own question.