6

I am trying to add phone number column in my order grid.

I have added new column through sales_order_grid.xml in my ui_component directory. Code is below:

<?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">
            <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>
    </columns>
</listing>

What should I do in collection to get phone number?

2
  • Would anyone know how to make the phone number searchable in sales order grid or admin search?
    – Shaun
    Feb 19, 2021 at 0:03
  • @Shaun check answer of Muhammad Hasham, from his way phone number will be available in grid and is searchable Feb 19, 2021 at 7:04

2 Answers 2

11

You added the column in ui_component correctly

Just replace the _renderFiltersBefore function with my function in your module collection class

Path should be Vendor/Yourmodule/Model/ResourceModel/Order/Grid/Collection.php

protected function _renderFiltersBefore()
{
 $joinTable = $this->getTable('sales_order_address');
 $this->getSelect()->joinLeft($joinTable, 'main_table.entity_id = 
 sales_order_address.parent_id AND sales_order_address.address_type = "shipping"', 
 ['telephone']);
 parent::_renderFiltersBefore();
}

In Case if you miss it don't forgot to add this argument in your dependency injection di.xml

<argument name="collections" xsi:type="array">
<item name="sales_order_grid_data_source" xsi:type="string">Vendor\Yourmodule\Model\ResourceModel\Order\Grid\Collection</item>
</argument>

I hope this will help

1
  • It does not work with "Search by keyword" Search Filed? which steps to add for it should also search via Search by keyword?
    – HaFiz Umer
    Jul 28, 2021 at 10:20
11

Please add the ui_component class for your column as per below.

<?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="Vendor\Module\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>
    </columns>
</listing>

Now please add Telephone.php as below

namespace Vendor\Module\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;
    }
}

Please check and let me know if any issue.

5
  • Thanks you for taking time in answering my question +1 Feb 11, 2019 at 10:25
  • 2
    @ShoaibMunir appreciated, yes accepted ans is much easier and better then mine :) Feb 11, 2019 at 10:31
  • @AasimGoriya with the above solution I am not able to filter orders using phone number.
    – GenZ Dev
    Mar 13, 2019 at 10:43
  • @AasimGoriya, I'm using magento 2.3.the above code working fine in the order listing, but it's becomes empty while export it as a csv file. any solution for that?
    – saravankg
    Jul 3, 2020 at 13:22
  • It does not work with "Search by keyword" Search Filed? which steps to add for it should also search via Search by keyword?
    – HaFiz Umer
    Jul 28, 2021 at 10:20

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.