0

How would I make the customers phone number searchable in admin for orders. Either in main admin search or the search bar in sales order grid. I have a custom column with the customers phone number.

sales_order_grid.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">
    <columns name="sales_order_columns">
        <column name="telephone" class="Fitzcode\AdminEdits\Ui\Component\Listing\Column\Telephone">
            <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">Phone Number</item>
                    <item name="sortOrder" xsi:type="number">6</item>
                </item>
            </argument>
        </column>
        <column name="comment" class="Fitzcode\AdminEdits\Ui\Component\Listing\Column\Comment">
            <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">Comment</item>
                    <item name="sortOrder" xsi:type="number">7</item>
                </item>
            </argument>
        </column>
    </columns>
</listing>

Telephone.php

<?php
namespace Fitzcode\AdminEdits\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 Telephone extends Column
{
    protected $_orderRepository;
    protected $_searchCriteria;

    public function __construct(ContextInterface $context, UiComponentFactory $uiComponentFactory, OrderRepositoryInterface $orderRepository, SearchCriteriaBuilder $criteria, array $components = [], array $data = [])
    {
        $this->_orderRepository = $orderRepository;
        $this->_searchCriteria  = $criteria;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {

                $order  = $this->_orderRepository->get($item["entity_id"]);
                $telephone = $order->getBillingAddress()->getTelephone();

                // $this->getData('name') returns the name of the column so in this case it would return export_status
                $item[$this->getData('name')] = $telephone;
            }
        }

        return $dataSource;
    }
}

Filter Phone Number

3
  • please post the code that you add for display that custom column – Jigs Parmar Feb 19 at 12:41
  • @JigsParmar See edit above. – Shaun Feb 20 at 22:40
  • Using the filter for phone number I am receiving the following error. Something went wrong with processing current custom view and filters have been reset to its original state. Please edit filters then click apply. – Shaun Feb 20 at 22:54
1

change code like below

<?php
namespace Fitzcode\AdminEdits\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 Telephone extends Column
{
    protected $_orderRepository;
    protected $_searchCriteria;

    public function __construct(ContextInterface $context, UiComponentFactory $uiComponentFactory, OrderRepositoryInterface $orderRepository, SearchCriteriaBuilder $criteria, array $components = [], array $data = [])
    {
        $this->_orderRepository = $orderRepository;
        $this->_searchCriteria  = $criteria;
        parent::__construct($context, $uiComponentFactory, $components, $data);
    }

    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items'])) {
            foreach ($dataSource['data']['items'] as & $item) {
                try{
                    $order  = $this->_orderRepository->get($item["entity_id"]);
                    if($order){
                        $billing = $order->getBillingAddress();
                        if($billing){
                            $telephone = $billing->getTelephone();
                            $item[$this->getData('name')] = $telephone;
                        }
                    }
                }catch (\Exception $e){
                    \Magento\Framework\App\ObjectManager::getInstance()->get('Psr\Log\LoggerInterface')->info($e->getMessage());
                }
            }
        }

        return $dataSource;
    }
}
1
  • This works for the main admin search but not for search by keyword in sales order grid. Also would you know why filter by phone number returns the error (Something went wrong with processing current custom view and filters have been reset to its original state. Please edit filters then click apply.) – Shaun Feb 23 at 22:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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