I have the following from another post, and now I'm looking for a way to make these 4 extra columns filterable.
Thank you,
\app\code\Vendor\SalesOrderGrid\Ui\Component\Listing\Column\Address.php
<?php
namespace Vendor\SalesOrderGrid\Ui\Component\Listing\Column;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\Ui\Component\Listing\Columns\Column;
use Magento\Framework\View\Element\UiComponent\ContextInterface;
use Magento\Framework\View\Element\UiComponentFactory;
/**
* Class Address
*/
class Address extends Column
{
/**
* @var OrderRepositoryInterface
*/
private $orderRepository;
/**
* Address constructor.
* @param ContextInterface $context
* @param UiComponentFactory $uiComponentFactory
* @param OrderRepositoryInterface $orderRepository
* @param array $components
* @param array $data
*/
public function __construct(
ContextInterface $context,
UiComponentFactory $uiComponentFactory,
OrderRepositoryInterface $orderRepository,
array $components = [],
array $data = []
) {
$this->orderRepository = $orderRepository;
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) {
$column = $this->getData('name');
$orderId = $item['entity_id'];
$order = $this->orderRepository->get($orderId);
if ($column == 'street') {
$streetArr = $order->getShippingAddress()->getStreet();
$data = implode(',', $streetArr);
} elseif ($column == 'zip') {
$data = $order->getShippingAddress()->getPostcode();
} elseif ($column == 'city') {
$data = $order->getShippingAddress()->getCity();
} elseif ($column == 'phone') {
$data = $order->getShippingAddress()->getTelephone();
}
$item[$this->getData('name')] = $data;
}
}
return $dataSource;
}
}
\app\code\Vendor\SalesOrderGrid\view\adminhtml\ui_component\sales_order_grid.xml
<column name="street" class="Vendor\SalesOrderGrid\Ui\Component\Listing\Column\Address">
<settings>
<filter>text</filter>
<label translate="true">Street</label>
<bodyTmpl>ui/grid/cells/html</bodyTmpl>
<visible>true</visible>
</settings>
</column>
<column name="zip" class="Vendor\SalesOrderGrid\Ui\Component\Listing\Column\Address">
<settings>
<filter>text</filter>
<label translate="true">Zip</label>
<bodyTmpl>ui/grid/cells/html</bodyTmpl>
<visible>true</visible>
</settings>
</column>
<column name="city" class="Vendor\SalesOrderGrid\Ui\Component\Listing\Column\Address">
<settings>
<filter>text</filter>
<label translate="true">City</label>
<bodyTmpl>ui/grid/cells/html</bodyTmpl>
<visible>true</visible>
</settings>
</column>
<column name="phone" class="Vendor\SalesOrderGrid\Ui\Component\Listing\Column\Address">
<settings>
<filter>text</filter>
<label translate="true">Phone</label>
<bodyTmpl>ui/grid/cells/html</bodyTmpl>
<visible>true</visible>
</settings>
</column>