I have a customer attribute named business_name on customer registration page. It is a customer attribute with type varchar.
I need to display this attribute in Sales >> Order sales_order_grid. How can I display the customer attribute in Order gird?
Any help will be appreciated
3 Answers 3
Create sales_order_grid.xml inside adminhtml > ui_component directory
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
<columns name="sales_order_columns">
<column name="business_name" class="Vendor\Module\Ui\Component\Listing\Column\BusinessName">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="bodyTmpl" xsi:type="string">text</item>
<item name="label" translate="true" xsi:type="string">Attribute Title</item>
<item name="sortable" xsi:type="boolean">true</item>
<item name="sortOrder" xsi:type="number">25</item>
</item>
</argument>
</column>
</columns>
</listing>
Then create a class inside Vendor\Module\Ui\Component\Listing\Column\BusinessName.php
<?php
namespace Vendor\Module\Ui\Component\Listing\Column;
use Magento\Customer\Model\CustomerFactory;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Framework\UrlInterface;
use Magento\Framework\View\Asset\Repository;
class BusinessName extends Column
{
/**
* @var Magento\Customer\Model\CustomerFactory $block
*/
protected $customerFactory;
/**
* @var \Magento\Framework\View\Asset\Repository
*/
protected $assetRepository;
/**
* @var Magento\Framework\UrlInterface
*/
private $urlBuilder;
/**
* @param Repository $assetRepository
* @param ContextInterface $context
* @param UiComponentFactory $uiComponentFactory
* @param CustomerFactory $customerFactory
* @param UrlInterface $urlBuilder
* @param array $data
*/
public function __construct(
Repository $assetRepository,
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
CustomerFactory $customerFactory,
UrlInterface $urlBuilder,
array $components = [], array $data = [])
{
$this->assetRepository = $assetRepository;
$this->customerFactory = $customerFactory;
$this->urlBuilder = $urlBuilder;
parent::__construct($context, $uiComponentFactory, $components, $data);
}
public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
foreach ($dataSource['data']['items'] as & $item) {
$customer = $this->customerFactory->create()->load($item["entity_id"]);
$businessName = $customer->getBusinessName();
if($businessName != ''){
$item[$this->getData('name')] = $businessName;
}
}
}
return $dataSource;
}
}
Let me know if have any query.
Please try below code by override sales_order_grid.xml to your own module:-
<columns name="sales_order_columns" class="Magento\Catalog\Ui\Component\Listing\Columns">
<column name="full_name" class="Sales\Custom\Ui\Component\Listing\Column\Fullname">
<argument name="data" xsi:type="array">
<item name="config" xsi:type="array">
<item name="filter" xsi:type="string">text</item>
<item name="label" xsi:type="string" translate="true">Full Name</item>
</item>
</argument>
</column>
</columns>
Now create the file at Sales\Custom\Ui\Component\Listing\Column\Fullname.php.
Your filename will be different this as was my requirement
<?php
namespace Sales\Custom\Ui\Component\Listing\Column;
use \Magento\Sales\Api\OrderRepositoryInterface;
use \Magento\Framework\View\Element\UiComponent\ContextInterface;
use \Magento\Framework\View\Element\UiComponentFactory;
use \Magento\Ui\Component\Listing\Columns\Column;
use \Magento\Framework\Api\SearchCriteriaBuilder;
class Fullname extends Column
{
public function prepareDataSource(array $dataSource)
{
if (isset($dataSource['data']['items'])) {
foreach ($dataSource['data']['items'] as & $item) {
$order = $item["entity_id"];
//Apply your algorithm here to fetch product attr from order id / you can also
}
}
return $dataSource;
}
}
I would have tried this when you create or update your attribute
'is_used_in_grid' => true,
'is_visible_in_grid' => true
If you already did that; may be you just miss the reindex process ?