3

I need to create customer custom attribute as datetime in magento 2.3

Below is my code into Setup/Patch/Data/AddLastPasswordChangeCustomerAttribute.php with apply() funcation

public function apply()
{
    $this->moduleDataSetup->getConnection()->startSetup();
    /** @var EavSetup $eavSetup */
    $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]);
    $eavSetup->addAttribute(
        \Magento\Customer\Model\Customer::ENTITY,
        'last_password_change',
        [
            'type' => 'varchar',
            'label' => 'last_password_change',
            'input' => 'text',
            'source' => '',
            'frontend' => '',
            'required' => false,
            'backend' => '',
            'default' => null,
            'user_defined' => true,
            'unique' => false,
            'group' => 'General',
        ]
    );

    $this->moduleDataSetup->getConnection()->endSetup();
}

Also i need to display that attribute in admin side in customer account information.

I have tried but it's not display at customer account information in admin side.

2
  • You have to assign this field at to adminhtml customer form
  • Have to assign the Attribute set for this attribute

So, create a data patch and add below code.:

Code

<?php

namespace {VendorName}\{ModuleName}\Setup\Patch\Data;

use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Framework\Setup\Patch\PatchInterface;

class Abc implements DataPatchInterface
{
    /**
     * @var \Magento\Customer\Setup\CustomerSetupFactory
     */
    private $customerSetupFactory;
    /**
     * @var \Magento\Eav\Model\Entity\Attribute\SetFactory
     */
    private $attributeSetFactory;

    public function __construct(
        \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory,
        \Magento\Eav\Model\Entity\Attribute\SetFactory $attributeSetFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
        $this->attributeSetFactory = $attributeSetFactory;
    }

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

    /**
     * {@inheritdoc}
     */
    public function getAliases()
    {
        return [];
    }

    /**
     * {@inheritdoc}
     */
    public function apply()
    {
        $customerSetup = $this->customerSetupFactory->create();
        $customerEntityType = $customerSetup->getEavConfig()
            ->getEntityType(\Magento\Customer\Api\CustomerMetadataInterface::ENTITY_TYPE_CUSTOMER);
        $attributeSetId = $customerEntityType->getDefaultAttributeSetId();
        $attributeDefaultGroupId= $this->attributeSetFactory->create()->getDefaultGroupId($attributeSetId);


        $lastPasswordChangeAttribute = $customerSetup->getEavConfig()->getAttribute('customer', 'last_password_change');

        if($lastPasswordChangeAttribute->getId()){
            $lastPasswordChangeAttribute->setData(
                'attribute_set_id',
                $customerEntityType->getDefaultAttributeSetId()
            );
            $lastPasswordChangeAttribute->setData(
                'attribute_group_id',
                $attributeDefaultGroupId
            );
            $lastPasswordChangeAttribute->setData(
                'used_in_forms', [
                    'adminhtml_customer'
                ]
            );
            $lastPasswordChangeAttribute->save();            
        }

    }
}

After that, you need to Do indexing from Command line.

| improve this answer | |
  • Amit bera can you send all files for this. I want to use this approach for adding custom fields in registration form. – Yousaf Mar 12 at 7:06
  • I want to pass attribute parameters. How it is possible? – Yousaf Mar 12 at 7:08
2

Please follow below InstallData script to add customer attribute and display in customer admin form. It is working for me.

<?php
namespace {VendorName}\{ModuleName}\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Customer\Model\Customer;
use Magento\Eav\Model\Entity\Attribute\Set as AttributeSet;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;
use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class InstallData implements InstallDataInterface{

const LAST_PASSWORD_CHAGNE = 'last_password_change';

/**
 * @var EavSetupFactory
 */
private $eavSetupFactory;


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

/**
 * @var AttributeSetFactory
 */
private $attributeSetFactory;

/**
 * @param CustomerSetupFactory $customerSetupFactory
 * @param AttributeSetFactory $attributeSetFactory
 */
public function __construct(
    EavSetupFactory $eavSetupFactory,
    CustomerSetupFactory $customerSetupFactory,
    AttributeSetFactory $attributeSetFactory
) {
    $this->eavSetupFactory = $eavSetupFactory;
    $this->customerSetupFactory = $customerSetupFactory;
    $this->attributeSetFactory = $attributeSetFactory;
}
    /**
     * {@inheritdoc}
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        $setup->startSetup();

        /**
         *  Customer attributes
         */
        /** @var CustomerSetup $customerSetup */
        $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);


        /**
         * Create customer attribute LAST_PASSWORD_CHAGNE
         */
        $customerSetup->addAttribute(Customer::ENTITY, self::LAST_PASSWORD_CHAGNE,
            [
                'type' => 'varchar',
                'label' => 'Last Password Change',
                'input' => 'text',
                'required' => false,
                'visible' => true,
                'user_defined' => true,
                'sort_order' => 215,
                'position' => 215,
                'system' => false,
            ]);
        $approve_account = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, self::LAST_PASSWORD_CHAGNE)
            ->addData([
                'attribute_set_id' => $attributeSetId,
                'attribute_group_id' => $attributeGroupId,
                'used_in_forms' => ['adminhtml_customer'],
            ]);
        $approve_account->save();

        $setup->endSetup();
    }
}
| improve this answer | |
  • You shouldn't use install scripts from Magento 2.3 but Data patches. This shouldn't be the accepted answer – Daniele Rovatti Jun 24 at 14:41

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.