6

I successfully add customer custom attribute for magento 2.2.5 like this:

$setup->startSetup();

$insertArray = array(
            'profile_picture' => array("type" => "varchar", "label" => "Profile Picture"),
            'security_question_1' => array("type" => "int", "label" => "Security Question 1"),
            'security_question_2' => array("type" => "int", "label" => "Security Question 2"),
            'security_answer_1' => array("type" => "varchar", "label" => "Security Answer 1"),
            'security_answer_2' => array("type" => "varchar", "label" => "Security Answer 2")
          );

          foreach ($insertArray as $key => $data) {
            $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

            $customerEntity = $customerSetup->getEavConfig()->getEntityType('customer');
            $attributeSetId = $customerEntity->getDefaultAttributeSetId();

            /** @var $attributeSet AttributeSet */
            $attributeSet = $this->attributeSetFactory->create();
            $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);

            $customerSetup->addAttribute(Customer::ENTITY, $key, [
                'type' => $data['type'],
                'label' => $data['label'],
                'input' => 'text',
                'required' => false,
                'visible' => true,
                'user_defined' => true,
                'sort_order' => 1000,
                'position' => 1000,
                'system' => 0,
            ]);

            $attribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, $key)
            ->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' => ['adminhtml_customer'],
            ]);

            $attribute->save();
          }
            $setup->endSetup();

I can retrieve and set the value programmatically, but the problem is when i edit customer in backend and go to account information tab, it doesn't load the input field for my custom attribute? if there is something missing, how can i update the customer custom attribute?

2 Answers 2

16

In contrast to Magento 1, in Magento 2 you now need to add some XML to make custom attributes appear in the edit customer section of the admin.

Create YourNamespace/YourModule/view/base/ui_component/customer_form.xml and add this (at a minimum):

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <fieldset name="customer">
        <field name="{{YOUR_ATTRIBUTE_CODE}}" formElement="input">
            <settings>
                <visible>true</visible>
            </settings>
        </field>
    </fieldset>
</form>

Replace {{YOUR_ATTRIBUTE_CODE}} with the attribute code from your upgrade script.

Run php -f bin/magento setup:upgrade and then you're good to go.

5
  • how to add select yes/no input field? Commented Jun 26, 2018 at 3:54
  • That's really a new question. Ask that as a new question and I'll answer it. There's not enough room to explain here Commented Jun 26, 2018 at 8:31
  • @ProcessEight thank you Sir! You saved my life! beer++
    – Radu
    Commented Sep 25, 2018 at 15:55
  • @TinyDancer in my case I needed a multiselect so I've set it like this: formElement="multiselect"
    – Radu
    Commented Sep 25, 2018 at 15:56
  • I don't think you need to add field to form manually in xml. As of magento 2.3.1 everything working for me with that approach magento.stackexchange.com/a/277271/23264
    – electroid
    Commented Feb 4, 2020 at 20:43
1

ProcessEight answer is not working for us. The field now is visible if u set 'user_defined' to true, but it cannot be saved in backend as well. After reloading customer in backend the field is empty...

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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