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.

Kushal Dani
  • 2,114
  • 2
  • 17
  • 49
Bhavin Pethani
  • 563
  • 2
  • 19

3 Answers3

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.

Amit Bera
  • 77,456
  • 20
  • 123
  • 237
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();
    }
}
Bhavin Pethani
  • 563
  • 2
  • 19
1

Create a new data patch with

*

bin/magento setup:db-declaration:generate-patch Module_Vendor CreateCustomerAttribute

Inject

• \Magento\Eav\Setup\EavSetupFactory to create the attribute
• \Magento\Eav\Model\Config to easily load the attribute
• \Magento\Customer\Model\ResourceModel\Attribute to save the attribute

Create data patch

<?php
namespace Module\Vendor\Setup\Patch\Data;

use Magento\Customer\Model\Customer as CustomerModel; use Magento\Customer\Model\ResourceModel\Attribute as AttributeResource; use Magento\Eav\Model\Config as EavConfig; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\Patch\DataPatchInterface;

class CreateCustomerAttribute implements DataPatchInterface { /** @var ModuleDataSetupInterface / private $moduleDataSetup; /* @var EavSetupFactory / private $eavSetupFactory; /* @var EavConfig / private $eavConfig; /* @var AttributeResource */ private $attributeResource; public function __construct( ModuleDataSetupInterface $moduleDataSetup, EavSetupFactory $eavSetupFactory, EavConfig $eavConfig, AttributeResource $attributeResource ) { $this->moduleDataSetup = $moduleDataSetup; $this->eavSetupFactory = $eavSetupFactory; $this->eavConfig = $eavConfig; $this->attributeResource = $attributeResource; } public function apply() { $eavSetup = $this->eavSetupFactory->create(['setup' => $this->moduleDataSetup]); $eavSetup->addAttribute( CustomerModel::ENTITY, 'middle_name', [ 'type' => 'varchar', 'label' => 'Middle name', 'input' => 'text', 'required' => false, 'visible' => true, 'user_defined' => true, 'position' => 999, 'system' => 0, ] ); $attribute = $this->eavConfig->getAttribute( CustomerModel::ENTITY, 'middle_name' ); // #2 add to forms $attribute->setData('used_in_forms', ['adminhtml_ customer']); // #3 save $this->attributeResource->save($attribute); } public function getAliases() { return []; } public static function getDependencies() { return []; } }

Sarvesh Tiwari
  • 740
  • 12
  • 41