0

I have created an attribute type: Test but I want to display in the grid customer.

https://prnt.sc/aRyraGEP7kKf

6
  • Take a look at magento.stackexchange.com/questions/250944/… Jul 26, 2022 at 21:29
  • I am using customer attribute module for custom attributes.
    – kiran
    Jul 26, 2022 at 23:48
  • @kiran you have created customer attribute using UpradeData.php Or using any module to create attribute? Jul 27, 2022 at 4:47
  • I am not using UpgradeData.php
    – kiran
    Jul 27, 2022 at 5:59
  • Using module to create attributes
    – kiran
    Jul 27, 2022 at 5:59

1 Answer 1

0

Create customer_listing.xml

app/code/Vendor/Module/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="company_name" class="Vendor\Module\Ui\Component\Listing\Column\CustomerAttribute">
            <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">Column Name</item>
                    <item name="sortOrder" xsi:type="number">40</item>
                    </item>
            </argument>
        </column>
    </columns>
</listing>

Then create CustomerAttribute.php

app/code/Vendor/Module/Ui/Component/Listing/Column/CustomerAttribute.php

<?php
namespace Vendor\Module\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 CustomerAttribute 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->getCompanyName();
            }
        }
        return $dataSource;
    }
}

Change customer_attribute_name according to yours

Hope this will help you! Let me know if you have any query.

8
  • field is visible in grid but value is not visible in customer grid. prnt.sc/fnA0L3pwrHf0
    – kiran
    Jul 27, 2022 at 16:49
  • @kiran you used above code? If yes then please share attribute_code I'll update the answer accordingly Jul 28, 2022 at 2:06
  • Attribute code : company_name
    – kiran
    Jul 28, 2022 at 3:52
  • @kiran I have updated my answer please try again and it will work for you. Please use the above code Jul 28, 2022 at 9:27
  • Thank you so much Arun Sharma
    – kiran
    Jul 28, 2022 at 13:26

Your Answer

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

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