3

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

2 Answers 2

2

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;
    }
}
11
  • Can you please help me what other files do i need create to create an extension for this functionality
    – Anil Singh
    Nov 24, 2017 at 11:31
  • Do i require Model's also?
    – Anil Singh
    Nov 24, 2017 at 11:32
  • No.. You just need xml and collection class as mentioned in answer
    – Shyam
    Nov 24, 2017 at 11:37
  • You Code added grid with Compnay name, But i can't get COmpany Names wrt to user's. In customer_grid_flat table field name is billing_company so i changed code to this parent::_initSelect(); $this->joinAttribute( 'billing_company', 'customer_address/billing_company', 'default_billing', null, 'left' ); bit it's not working
    – Anil Singh
    Nov 24, 2017 at 11:52
  • the same code will work if the customer already has an address saved with the company information. please share your Magento version.
    – Shyam
    Nov 24, 2017 at 13:16
1

If anyone is looking for the answer to this question, I was able to get everything working.

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(
            'billing_company',
            'customer_address/company',
            'default_billing',
            null,
            'left'
        );
        return $this;
    }
}

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">Trucatch\Sales\Model\ResourceModel\Order\Customer\Collection</argument>
            </arguments>
            <referenceBlock name="adminhtml.customer.grid.columnSet">
                <block class="Magento\Backend\Block\Widget\Grid\Column" as="billing_company" after="name">
                    <arguments>
                        <argument name="header" xsi:type="string" translate="true">Company</argument>
                        <argument name="index" xsi:type="string">billing_company</argument>
                    </arguments>
                </block>
            </referenceBlock>
        </referenceBlock>
    </body>
</page>

The difference between this solution and @Shyam is the second referenceBlock needs to be within the first referenceBlock.

1
  • can any buddy share complete module.. to add company name is sales order coloumn
    – irshad
    Jan 9, 2020 at 7:59

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.