Magento – Update custom columns in sales_order_grid / Regenerate sales_order_grid

backendgridindexmagento2

I have a custom module that adds an extra column to sales_order which is updated at a point after the order is made.

I have in my upgradeShcema the following code

  $setup->getConnection()->addColumn(
    $setup->getTable('sales_order'),
    'epicor_order_id',
    [
      'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
      'nullable' => false,
      'comment' => 'Epicor Order ID',
    ]
  );
  $setup->getConnection()->addColumn(
    $setup->getTable('sales_order_grid'),
    'epicor_order_id',
    [
      'type' => \Magento\Framework\DB\Ddl\Table::TYPE_INTEGER,
      'nullable' => false,
      'comment' => 'Epicor Order ID',
    ]
  );
}

This works and I can see the two extra columns in the database

Some time after the order is created I am running this code.

    $orderObject = $this->_orderRepository->get($epicorOrder->getOrderId());

    if($orderObject->getId()){
      $orderObject->setEpicorOrderId($epicor_order_id)->save();
    }

I can see this is correctly updating the column in the sales_order table but it is not updating the order in the sales_order_grid table.

I am also getting this column to display in admin view via this file in Vendor/Module/view/adminhtml/ui_component/sales_order_grid.xml

This displays the value I want but the value isn't being updated. If I manually go into the database and change the column I can see the change is visible in the admin view.

<?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="epicor_order_id">
      <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
          <item name="filter" xsi:type="string">text</item>
          <item name="label" xsi:type="string" translate="true">Epicor Order ID</item>
        </item>
      </argument>
    </column>
  </columns>
</listing>

I have checked cron job, reindexed via command line, cleared cache. I also tried fiddling with asynchronous indexing as mentioned in this question Rebuilding sales_order_grid and tried deleting records out of the sales_order_grid table. Even when the table is regenerated the epicor_order_id is not copied across.

Best Answer

You have to add this code to your etc/di.xml, inside config tag

<virtualType name="Magento\Sales\Model\ResourceModel\Order\Grid" type="Magento\Sales\Model\ResourceModel\Grid">
        <arguments>
            <argument name="columns" xsi:type="array">
                <item name="epicor_order_id" xsi:type="string">sales_order.epicor_order_id</item>
            </argument>
        </arguments>
</virtualType>

After you add this code execute those command

php bin/magento setup:upgrade

php bin/magento setup:di:compile

This how the file should look like

{Vendor}/{Module}/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <virtualType name="Magento\Sales\Model\ResourceModel\Order\Grid" type="Magento\Sales\Model\ResourceModel\Grid">
        <arguments>
            <argument name="columns" xsi:type="array">
                <item name="epicor_order_id" xsi:type="string">sales_order.epicor_order_id</item>
            </argument>
        </arguments>
    </virtualType>
</config>