2

how I can add a custom attribute for customer

using DB schema in Magento 2.3

does anyone is already tried

Just help me?

3 Answers 3

3

You can use the below code in your module's Setup/InstallData.php

<?php
namespace Vendor\CustomerAttribute\Setup;

use Magento\Framework\Setup\InstallDataInterface;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Customer\Model\Customer;
use Magento\Customer\Setup\CustomerSetupFactory;

class InstallData implements InstallDataInterface
{
    private $customerSetupFactory;
    /**
     * Constructor
     *
     * @param \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
     */
    public function __construct(
        CustomerSetupFactory $customerSetupFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public function install(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        $customerSetup = $this->customerSetupFactory->create(['setup' => $setup]);

        $customerSetup->addAttribute(\Magento\Customer\Model\Customer::ENTITY, 'custom_telephone', [
            'type' => 'varchar',
            'label' => 'custom_telephone',
            'input' => 'text',
            'source' => '',
            'required' => true,
            'visible' => true,
            'position' => 333,
            'system' => false,
            'backend' => ''
        ]);

        $attribute = $customerSetup->getEavConfig()->getAttribute('customer', 'custom_telephone')
        ->addData(['used_in_forms' => [
                'adminhtml_customer',
                'adminhtml_checkout',
                'customer_account_create',
                'customer_account_edit'
            ]
        ]);
        $attribute->save();
    }
}

Hope it will work for you.

4
3

You need to use data patch (DataPatchInterface), instead of db-schmena,

Db-scheme basically used for database table create, table structure changes.

An example:

<?php


namespace Devbera\ExamWork\Setup\Patch\Data;


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

use Magento\Framework\Setup\Patch\PatchVersionInterface;

class CreateCustomAttribute implements  DataPatchInterface ,\Magento\Framework\Setup\Patch\PatchVersionInterface
{


    /**
     * @var \Magento\Customer\Setup\CustomerSetupFactory
     */
    private $customerSetupFactory;

    public function __construct(
        \Magento\Customer\Setup\CustomerSetupFactory $customerSetupFactory
    ) {
        $this->customerSetupFactory = $customerSetupFactory;
    }

    /**
     * {@inheritdoc}
     */
    public static function getVersion()
    {
        return  '2.3.1';
    }


    /**
     * Get array of patches that have to be executed prior to this.
     *
     * example of implementation:
     *
     * [
     *      \Vendor_Name\Module_Name\Setup\Patch\Patch1::class,
     *      \Vendor_Name\Module_Name\Setup\Patch\Patch2::class
     * ]
     *
     * @return string[]
     */
    public static function getDependencies()
    {
        return [];
    }

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

    /**
     * Run code inside patch
     * If code fails, patch must be reverted, in case when we are speaking about schema - than under revert
     * means run PatchInterface::revert()
     *
     * If we speak about data, under revert means: $transaction->rollback()
     *
     * @return $this
     */
    public function apply()
    {
        $customerSetup = $this->customerSetupFactory->create();
        $customerSetup->addAttribute(
            \Magento\Customer\Model\Customer::ENTITY,
            'referral_code',
            [
                'type' => 'varchar',
                'label' => 'Referral Code',
                'input' => 'text',
                'sort_order' => 250,
                'required' => false,
                'system' => false,
                'visible' => true,
                'user_defined' => true,
            ]
        );
    }


}
2
  • this one also achieve same output right... Dec 17, 2019 at 6:25
  • yes, this will provide the same output.
    – Amit Bera
    Dec 17, 2019 at 8:08
3

It can be achieved using DataPatchInterface

namespace Vendor_Name\Module_Name\Setup\Patch\Data; 

use Magento\Customer\Setup\CustomerSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\Patch\DataPatchInterface;
use Magento\Eav\Model\Entity\Attribute\SetFactory as AttributeSetFactory;

class BuiltEav implements DataPatchInterface {

private $moduleDataSetup;
protected $customerSetupFactory;
private $eavSetupFactory;

public function __construct(
    ModuleDataSetupInterface $moduleDataSetup,
    CustomerSetupFactory $customerSetupFactory,
    AttributeSetFactory $attributeSetFactory
) {
    $this->moduleDataSetup = $moduleDataSetup;
    $this->attributeSetFactory = $attributeSetFactory;
    $this->customerSetupFactory = $customerSetupFactory;
}
public static function getDependencies()
{
    return [];
}
public function getAliases()
{
    return [];
}
public function apply()
{
    $customerSetup = $this->customerSetupFactory->create(['setup' => $this->moduleDataSetup]);

    $customerEntity = $customerSetup->getEavConfig()->getEntityType(Customer::ENTITY);
    $attributeSetId = $customerEntity->getDefaultAttributeSetId();

    $attributeSet = $this->attributeSetFactory->create();
    $attributeGroupId = $attributeSet->getDefaultGroupId($attributeSetId);


    $customerSetup->addAttribute(
        Customer::ENTITY,
        'mobile',
        [
            'type' => 'varchar',
            'label' => 'Mobile',
            'global' => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_GLOBAL,
            'input' => 'text',
            'required' => false,
            'sort_order' => 120,
            'position' => 120,
            'visible' => true,
            'user_defined' => true,
            'system' => false,
            'searchable' => true,
            'is_used_in_grid'   => true,
            'is_visible_in_grid' => true,
            'visible_in_advanced_search' => true,
            'is_searchable_in_grid' => true
        ]
    );
    $attribute = $customerSetup->getEavConfig()->getAttribute(
        Customer::ENTITY,
        'mobile'
    );

    $attribute->addData(
        [
            'attribute_set_id' => $attributeSetId,
            'attribute_group_id' => $attributeGroupId,
            'used_in_forms' => ['adminhtml_customer'],
        ]
    );

    $attribute->save();
}
}
2

Your Answer

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

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