1

Magento 2 Add customer Billing Phone number column in Order and Invoice grid. I also want to filter and search with this phone number. Please provide code if anyone have done this.

3

First you need to add column through uicomponent

app\code\Vendor\Module\view\adminhtml\ui_component\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">
        <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>

then Add the collection file in your module

Vendor\Module\Model\ResourceModel\Order\Grid\Collection.php

<?php

namespace Vendor\Module\Model\ResourceModel\Order\Grid;

use Magento\Sales\Model\ResourceModel\Order\Grid\Collection as OriginalCollection;

/**
 * Order grid extended collection
 */
class Collection extends OriginalCollection
{

    protected function _initSelect()
    {
        $this->addFilterToMap('status', 'main_table.status');
        $this->addFilterToMap('customer_id', 'main_table.customer_id');

        parent::_initSelect();
    }

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

At last Add di.xml in your module

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
 <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
 <arguments>
 <argument name="collections" xsi:type="array">
 <item name="sales_order_grid_data_source" xsi:type="string">Vendor\Module\Model\ResourceModel\Order\Grid\Collection</item>
 </argument>
 </arguments>
 </type>
</config>

It will add customer billing phone number in order grid with filter. I wrote this code it is my tested code. I hope this will help

5
  • Will this work for Search by keyword also ? – Mohit chauhan Mar 13 '19 at 10:44
  • Check the screenshot: screencast.com/t/UGSHyQMKaG it will look like this. Text field filter because some times people added + in phone number as well it work on phone number column field – Muhammad Hasham Mar 13 '19 at 10:48
  • @Mohitchauhan if my answer helpful to you and fulfill to your need please mark my answer correct and upvote my answer so it helps others as well. keep coding :) thanks :) – Muhammad Hasham Mar 13 '19 at 10:50
  • Filter is working fine. but Search by keyword is not working. – Mohit chauhan Mar 13 '19 at 12:38
  • @Mohitchauhan you want to phone number filter and column. It added filter and column I don't get what do you mean by search by keyword and why it is needed? – Muhammad Hasham Mar 13 '19 at 12: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.