Magento – add Company name in Customer grid during order creation

magento2

I want to add Company Name in Customer grid during order creation. Can any one help me how can we do that in magento2.We need to extened sales_order_create_customer_block.xml, but i can't able make it. Please help me.
Here are my code screenshtots with output
enter image description here

enter image description here

enter image description here

Best Answer

Yes, you're right. We need to update this sales_order_create_customer_block.xml layout file for company attribute but alongside this we also need to extend the grid collection.

Because the collection, which is being used to populate customer grid data doesn't include company attribute by default.

So after adding company attribute to the collection, we can add a new block instance for company column in grid columnSet.

Here are the steps to do so in your custom module.

app/code/{Namespace}/{Module}/view/adminhtml/layout/sales_order_create_customer_block.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="adminhtml.customer.grid.container">
            <arguments>
                <argument name="dataSource" xsi:type="object">\{Namespace}\{Module}\Model\ResourceModel\Order\Customer\Collection</argument>
            </arguments>
        </referenceBlock>
        <referenceBlock name="adminhtml.customer.grid.columnSet">
            <block class="Magento\Backend\Block\Widget\Grid\Column" as="company" after="billing_postcode">
                <arguments>
                    <argument name="header" xsi:type="string" translate="true">Company</argument>
                    <argument name="index" xsi:type="string">company</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

app/code/{Namespace}/{Module}/Model/ResourceModel/Order/Customer/Collection.php

<?php
namespace {Namespace}\{Module}\Model\ResourceModel\Order\Customer;

class Collection extends \Magento\Sales\Model\ResourceModel\Order\Customer\Collection
{
    /**
     * @return $this
     */
    protected function _initSelect()
    {
        parent::_initSelect();
        $this->joinAttribute(
            'company',
            'customer_address/company',
            'default_billing',
            null,
            'left'
        );
        return $this;
    }
}
Related Topic