Magento – Magento 2 – Custom Customer attribute in order create select customer grid

attributescustomer-gridgridlayoutmagento2

I am trying to add a custom column in sales create order customer grid.
When admin trying to place order from back end ,after create order button click we get a customer grid .

enter image description here

I have added a new column Company Name to this grid using below code in

sales_order_create_customer_block.xml

<block class="Magento\Backend\Block\Widget\Grid\Column" name="adminhtml.customer.grid.columnSet.company_name" as="company_name">
                        <arguments>
                            <argument name="header" xsi:type="string" translate="true">Company Name</argument>
                            <argument name="index" xsi:type="string">company_name</argument>
                            <argument name="align" xsi:type="string">center</argument>
                        </arguments>
                    </block>

Here Company Name is a custom customer attribute I have created .

After adding this I am getting the company name in this grid.
But the company name does not show at first load.
In the above screenshot for customer Id 66, I have set the company name still its displayed none.When I filter it by clicking on company name header it filters and then the company name is displayed for each customer and properly.

enter image description here

And when I reload the page it again displays nothing and I have to filter it again to display it .
I tried giving a different customer field gender in the xml and I am getting Boolean values as expected and no need of filter needed to display it like in case of my custom customer attribute.
Is there any that I have missed out in configuring this ?

Best Answer

I tried this and it worked for me. And i'm sure it will work for you too.

Add this code in your module (Vendor\Module\Model\ResourceModel\Order\Customer\Collection.php)

<?php
namespace Vendor\Module\Model\ResourceModel\Order\Customer;

class Collection extends \Magento\Sales\Model\ResourceModel\Order\Customer\Collection
{
    /**
     * @return $this
     */
    protected function _initSelect()
    {
        parent::_initSelect();
        $this->addAttributeToSelect(
            'company_name'
        );
        return $this;
    }
}

Make sure that you add this code in the same module where you have overrided sales_order_create_customer_block.xml

Thanks

Related Topic