0

I want to create a custom customer attribute where for example he can complete custom values for

  • Complete your 5 best movies

Multiselect does not work because they need a source. There is something that I can create to save that custom data for example in a JSON or so?

1

hi @martin to create custom customer attribute you have write Installdata.php within MyCompany/MyModule/Setup

<?php

 namespace MyCompany\MyNewAttribute\Setup;

 use Magento\Customer\Model\Customer;
 use Magento\Eav\Setup\EavSetupFactory;
 use Magento\Framework\Setup\InstallDataInterface;
 use Magento\Framework\Setup\ModuleContextInterface;
 use Magento\Framework\Setup\ModuleDataSetupInterface;
 use MyCompany\MyNewAttribute\Model\Entity\Attribute\Source\MyNewAttribute;
 use Magento\Customer\Setup\CustomerSetupFactory;
 use Magento\Eav\Model\Config as EavConfig;

 class InstallData implements InstallDataInterface
 {
/**
 * @var EavSetupFactory
 */
protected $eavSetupFactory;

/**
 * @var EavConfig
 */
private $eavConfig;

/**
 * @var CustomerSetupFactory
 */
private $customerSetupFactory;

/**
 *
 * @param EavSetupFactory $eavSetupFactory
 */
public function __construct(
    EavSetupFactory $eavSetupFactory,
    EavConfig $eavConfig,
    CustomerSetupFactory $customerSetupFactory
) {
    $this->eavSetupFactory = $eavSetupFactory;
    $this->eavConfig = $eavConfig;
    $this->customerSetupFactory = $customerSetupFactory;
}

/**
 *
 * @param ModuleDataSetupInterface $setup
 * @param ModuleContextInterface $context
 */
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
    $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
    $eavSetup->addAttribute(
        Customer::ENTITY,
        'my_new_attribute',
        [
            'type' => 'text',
            'label' => 'My New Attribue',
            'input' => 'multiselect',
            'option' => ['values' => [
                           'Option 1',
                           'Option 2',
                           'Option 3',
                           ],
                        ],
            'required' => false,
            'system' => 0
        ]
    );

    $usedInForms = [
        'adminhtml_customer',
        'customer_account_create'
    ];

    $attribute = $this->eavConfig->getAttribute(Customer::ENTITY, 'my_new_attribute');
    $attribute->setData('used_in_forms', $usedInForms);
    $attribute->save();
}}

Then run setup:upgrade command and check attribute in customer form.

Please accept & Vote answer if it is helpful. Thanks

1
  • No, i know how to create a custom attribute but i asked for an specific one where you can have options on the fly in a form. Maybe a json custom attribute or something like that were you can store dinamic things no preset options
    – martin
    Feb 11 '20 at 23:31

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.