magento 2 add company column to sale order invoice grid in backend

If you want add a custom column to sale order invoice grid, you need set value for column in data soruce of ui component.

So here is an example: how to add company column to sale order invoice grid in backend ?

1. Add “company” column to sales_order_invoice_grid in view.

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
  3. <columns name="sales_order_invoice_columns">
  4. <column name="company">
  5. <argument name="data" xsi:type="array">
  6. <item name="config" xsi:type="array">
  7. <item name="filter" xsi:type="string">text</item>
  8. <item name="dataType" xsi:type="string">text</item>
  9. <item name="label" xsi:type="string" translate="true">Company</item>
  10. <item name="sortOrder" xsi:type="number">70</item>
  11. </item>
  12. </argument>
  13. </column>
  14. </columns>
  15. </listing>

2. Create di.xml for “company” value where to get.

  1. <?xml version="1.0"?>
  2. <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  3. <type name="Magento\Framework\View\Element\UiComponent\DataProvider\CollectionFactory">
  4. <arguments>
  5. <argument name="collections" xsi:type="array">
  6. <item name="sales_order_invoice_grid_data_source" xsi:type="string">Tony\InvoiceCompanyColumn\Model\ResourceModel\Order\Invoice\Grid\Collection</item>
  7. </argument>
  8. </arguments>
  9. </type>
  10. </config>

3. Write some logic to model.

  1. protected function _renderFiltersBefore()
  2. {
  3. $joinTable = $this->getTable('sales_order_address');
  4. $this->getSelect()->joinLeft($joinTable,
  5. 'main_table.order_id = sales_order_address.parent_id AND sales_order_address.address_type = "billing"',
  6. ['company']);
  7. parent::_renderFiltersBefore();
  8. }

After completing above steps you will see a new column named “company” will show in sale order invoice grid.
Tony's blog image