0

I want to add the company name to the customer grid, when you create a new order in the admin panel.

Therefore I created the following module, but there is no field with company name added to the grid.

What am I missing here?

app/code/Vendor/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">\Vendor\ 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/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->joinAttribute(
            'company',
            'customer_address/company',
            'default_billing',
            null,
            'left'
        );
        return $this;
    }
}
1
  • looks like my answer is identical to your code.... I have updated my module, so you are welcome to try my module. However, I admit I can't see any major difference. But my module does work on my local Commented Jun 15, 2020 at 16:09

2 Answers 2

1

step 1: add your column into the ui_component file sales_order_grid.xml

app/code/Mbs/CompanyName/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="company" >
            <settings>
                <sortable>true</sortable>
                <filter>text</filter>
                <label translate="true">Company</label>
            </settings>
        </column>
    </columns>
</listing>

step 2: create di.xml

app/code/Mbs/CompanyName/etc/di.xml

<?xml version="1.0" encoding="utf-8" ?>
    <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">
            <plugin name="grid_order_company_name" type="Mbs\CompanyName\Plugin\JoinCompanyName" sortOrder="5" />
        </type>
    </config>

step 3: create an admin plugin that feeds the column with the value from the sales_order_address table

app/code/Mbs/CompanyName/Plugin/JoinCompanyName.php

<?php

namespace Mbs\CompanyName\Plugin;

use Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory;

class JoinCompanyName
{
    public function afterGetReport(
        CollectionFactory $subject,
        $collection,
        $requestName
    ) {
        if ($requestName == 'sales_order_grid_data_source') {
            $select = $collection->getSelect();
            $select->joinLeft(
                ['customer_address' => $collection->getTable('sales_order_address')],
                'main_table.entity_id = customer_address.parent_id and address_type="billing"',
                ['company']
            );
        }
        return $collection;
    }
}

Full module below: https://bitbucket.org/magstaging/companyname/src/master/

2
  • Thanks Herve! That's nice, but not it is added to the sales order grid. But we need it at the create new order grid. That's the grid when you click the orange button create new order from the order grid.
    – JGeer
    Commented Jun 15, 2020 at 15:30
  • looks like you were really close, <argument name="index" xsi:type="string">company</argument> should read <argument name="index" xsi:type="string">billing_company</argument> Commented Jun 15, 2020 at 16:03
0

To resolve this issues, we can separate the problem in 2 steps:

step 1: make the column appearing

create a new module and add the layout file 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.columnSet">
            <block class="Magento\Backend\Block\Widget\Grid\Column" name="adminhtml.customer.grid.columnSet.company" as="Company">
                <arguments>
                    <argument name="header" xsi:type="string" translate="true">Company</argument>
                    <argument name="index" xsi:type="string">billing_company</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

step2: feed the column with data take over the collection that feed the column

insert the xml snippet

<referenceBlock name="adminhtml.customer.grid.container">
                <arguments>
                    <argument name="dataSource" xsi:type="object">Mbs\CompanyName\Model\ResourceModel\Order\Customer\Collection</argument>
                </arguments>
            </referenceBlock>

and then add the collection file

<?php

namespace Mbs\CompanyName\Model\ResourceModel\Order\Customer;

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

        return $this;
    }
}
1
  • Thanks! I just tried it again, and there is no company name field added to the admin, when clicking the create new order button. What am I missing here?
    – JGeer
    Commented Jun 16, 2020 at 6:47

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.