Magento 2 Add Customer Fullname on Admin Grid
While developing any custom extension most of the time we need to add some features which depend on the customer and in that case, we'll store the customer id in our custom table and want to display the customer's full name in the admin grid. So, in this article, we'll show you how to display the customer's full name on the admin UI grid.
Before we start I assume, you have already created a custom grid collection.
Now, you just need to add _initSelect() and addFieldToFilter functions in your collection class file.
saveCopyzoom_out_mapprotected function _initSelect()
{
parent::_initSelect();
$this->getSelect()->join(
['ce' => $this->getConnection()->getTableName('customer_entity')],
'main_table.customer_id = ce.entity_id',
[
'customer_name' => 'CONCAT(`ce`.`firstname`, " ", `ce`.`lastname`)'
]
);
return $this;
}
In the above code you can see we've get customer firstname and lastname from the customer_entity table usgin join.
saveCopyzoom_out_mappublic function addFieldToFilter($field, $condition = null)
{
if ($field === 'customer_name') {
$customerTable = $this->getConnection()->getTableName('customer_entity');
$this->getSelect()->joinLeft(
['cust' => $customerTable],
'main_table.customer_id = cust.entity_id',
[]
);
$conditionSql = $this->_getConditionSql(
'CONCAT(`cust`.`firstname`, " ", `cust`.`lastname`)',
$condition
);
$this->getSelect()->where($conditionSql);
return $this;
}
return parent::addFieldToFilter($field, $condition);
}
We've added addFieldToFilter function with join just because of filter.
Now, you just need to add customer_name as an column in your grid. Review below example code for your reference.
saveCopyzoom_out_map<column name="customer_name">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="dataType" xsi:type="string">select</item>
<item name="label" xsi:type="string" translate="true">Customer Name</item>
</item>
</argument>
</column>
Open Command line in folder root of magento and run the below commands.
saveCopyzoom_out_mapphp bin/magento setup:upgrade php bin/magento cache:clean
That's it.
We hope this Magento article helped you to know How to Add Customer Fullname on Admin UI Grid in Magento 2.
Bookmark it for your future reference. Do comment below if you have any other questions or doubts.
P.S. Do share this article with your team.
AI-Powered Recommended Articles
How to Create Custom Table and Insert Sample Data in Magento 2
Create custom tables in Magento 2 and insert sample data for testing or customization purposes.
Magento 2 Implement Declarative Schema in Custom Module.
A guide on implementing declarative schema in your custom Magento 2 modules to improve database management and scalability.
How to Insert Multiple Records using ResourceConnection in Magento 2
Learn how to insert multiple records efficiently in Magento 2 using resource connection to manage database operations effectively.
Magento 2 Add Customer Attribute Programmatically
Learn how to add custom customer attributes programmatically in Magento 2 for customized customer data.
Magento 2 - How to Add a Custom Filed in Order Grid
Learn how to add a custom column to the admin order grid in Magento 2 for enhanced order tracking.
How to Create UI Component Grid In Magento 2
Learn how to create a custom UI component grid in Magento 2 for enhanced administrative functionality.