Question

I have the following code to create a custom customer attribute.

<?php

namespace Vendor\MyModule\Setup\Patch\Data;

use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Customer\Setup\Patch\Data\UpdateIdentifierCustomerAttributesVisibility;

class AddCustomerAccountCode implements DataPatchInterface
{
    private $moduleDataSetup;

    private $customerSetupFactory;

    public function __construct(
        ModuleDataSetupInterface $moduleDataSetup,
        CustomerSetupFactory $customerSetupFactory
    ) {
        $this->moduleDataSetup = $moduleDataSetup;
        $this->customerSetupFactory = $customerSetupFactory;
    }

    public function apply()
    {
        $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $customerSetup->addAttribute(Customer::ENTITY, 'customer_account_code', [
            'type' => 'varchar',
            'label' => 'Account Code',
            'input' => 'text',
            'sort_order' => 100,
            'position' => 100,
            'required' => false,
            'visible' => true,
            'system' => false
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public static function getDependencies()
    {
        return [
            UpdateIdentifierCustomerAttributesVisibility::class,
        ];
    }

    public function getAliases()
    {
        return [];
    }
}

The custom attribute is added to the admin form however when I populate the field and save the customer, the data is not saved.

Please could someone point out where I have gone wrong

Was it helpful?

Solution

You have to enable this attribute for admin form:

public function apply()
    {
        $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);
        $customerSetup->addAttribute(Customer::ENTITY, 'customer_account_code', [
            'type' => 'varchar',
            'label' => 'Account Code',
            'input' => 'text',
            'sort_order' => 100,
            'position' => 100,
            'required' => false,
            'visible' => true,
            'system' => false
        ]);
        $customerAccountCode =$customerSetup->getEavConfig()->clear()
            ->getAttribute(Customer::ENTITY, 'customer_account_code');


        if ($customerAccountCode->getAttributeId()) {
            $usedInForms =  ['adminhtml_customer','customer_account_create'];
            $data = [];
            foreach ($usedInForms as $formCode) {
                $data[] = ['form_code' => $formCode, 'attribute_id' => $customerAccountCode->getAttributeId()];
            }
            $this->moduleDataSetup->getConnection()->insertMultiple(
                $this->moduleDataSetup->getTable('customer_form_attribute'),
                $data
            );
            $this->moduleDataSetup->getConnection()->endSetup();
        }

After that, you need to cache flush and indexing.

OTHER TIPS

Try adding a user_defined equal to true Param to your addAttribute call

'user_defined' => true,

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top