Our Work
200+ Enterprises across globally trust KTree for their Web & Mobile application Development needs.See What We Do
Updated today
This article will explains, how to create custom attribute to customer. In this article we have added phone number filed to the customer.
To create Phone Number custom attribute to customer we need to do the following activities.
Please refer this article for Module Creation.
https://ktree.com/create-magento-2-module-custom.html
This step is similar to Magento 1, to add new attribute we need to create script file inside Setup folder
File Location :- app/code/KTree/CustomerAttribute/Setup/InstallData.php
<?php
namespace KTree\CustomerAttribute\Setup;
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
{
/**
* @var CustomerSetupFactory
*/
protected $customerSetupFactory;
/**
* @var AttributeSetFactory
*/
private $attributeSetFactory;
/**
* @param CustomerSetupFactory $customerSetupFactory
* @param AttributeSetFactory $attributeSetFactory
*/
public function __construct(
CustomerSetupFactory $customerSetupFactory,
AttributeSetFactory $attributeSetFactory
) {
$this->customerSetupFactory = $customerSetupFactory;
$this->attributeSetFactory = $attributeSetFactory;
}
public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
{
/*customersetupfactory instead of eavsetupfactory */
$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 phone number attribute */
$customerSetup->addAttribute(Customer::ENTITY,'phone_number', [
'type' => 'varchar', // attribute with varchar type
'label' => 'Phone Number',
'input' => 'text', // attribute input field is text
'required' => false, // field is not required
'visible' => true,
'user_defined' => true,
'position' => 999,
'sort_order' => 999,
'system' => 0,
'is_used_in_grid' => 1, //setting grid options
'is_visible_in_grid' => 1,
'is_filterable_in_grid' => 1,
'is_searchable_in_grid' => 1,
]
);
$sampleAttribute = $customerSetup->getEavConfig()->getAttribute(Customer::ENTITY, 'phone_number')
->addData(
[
'attribute_set_id' => $attributeSetId,
'attribute_group_id' => $attributeGroupId,
'used_in_forms' => ['adminhtml_customer','customer_account_edit','customer_account_create'],
]
// more used_in_forms ['adminhtml_checkout','adminhtml_customer','adminhtml_customer_address','customer_account_edit','customer_address_edit','customer_register_address']
);
$sampleAttribute->save();
}
}
After completing above step:
Magento does not provide us with the ability to choose which attributes are included as columns in the customers grid but it’s fairly simple to make the necessary code changes.
File:-app/code/KTree/CustomerAttribute/view/adminhtml/ui_component/customer_listing.xml
<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<listingToolbar name="listing_top"/>
<columns name="customer_columns" class="Magento\Customer\Ui\Component\Listing\Columns">
<column name="phone_number" class="KTree\CustomerAttribute\Ui\Component\Listing\Column\PhoneNumber">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="editor" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Phone Number</item>
<item name="sortOrder" xsi:type="number">40</item>
</item>
</argument>
</column>
</columns>
</listing>
File:app/code/KTree/CustomerAttribute/Ui/Component/Listing/Column/PhoneNumber.php
<?php
namespace KTree\CustomerAttribute\Ui\Component\Listing\Column;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;
class PhoneNumber extends Column
{
/**
*
* @param ContextInterface $context
* @param UiComponentFactory $uiComponentFactory
* @param array $components
* @param array $data
*/
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
array $components = [],
array $data = []
) {
parent::__construct($context, $uiComponentFactory, $components, $data);
}
/**
* Prepare Data Source
*
* @param array $dataSource
* @return array
*/
public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
foreach ($dataSource['data']['items'] as & $item)
{
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customer = $objectManager->create('Magento\Customer\Model\Customer')->load($item['entity_id']);
$item[$this->getData('name')] = $customer->getPhoneNumber();
}
}
return $dataSource;
}
}
?>
Instead overriding entire registration.phtml file we used container name form.additional.info as reference and adding a new block to it.
File: app/code/KTree/CustomerAttribute/view/frontend/layout/customer_account_create.xml
<page
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="1column" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<body>
<referenceContainer name="form.additional.info">
<block class="Magento\Framework\View\Element\Template" name="phone_number" template="KTree_CustomerAttribute::phone_number.phtml"/>
</referenceContainer>
</body>
</page>
In phone_number.phtml file we will write the code to show the textbox
<div class="field phone_number">
<label class="label" for="phone_number"><span><?php /* @escapeNotVerified */ echo __('Phone Number') ?></span></label>
<div class="control">
<input type="text" id="phone_number" name="phone_number" title="<?php /* @escapeNotVerified */ echo __('Phone Number') ?>" class="input-text" autocomplete="off">
</div>
</div>
Please Contact us if you have any Magento Implementation requirements. Hire dedicated Magento developers or Magento Development services from KTree. KTree is Best offshore Magento E-commerce development company with extensive experience in E-commerce development and Magento Plugins.
Request For Quote