Magento 2 – How to Change Value in Admin Order Grid Column

admincustomgridmagento2

I want to change value in admin order grid table in "Grand Total (Base)" column. Just add some value to it. I assume I need to create sales_order_grid.xml in my module and create block with prepareDataSource() function like when I adding new column to grid. But how to override the existing one?

So in original xml we have:

<column name="base_grand_total" class="Magento\Sales\Ui\Component\Listing\Column\Price">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="filter" xsi:type="string">textRange</item>
            <item name="label" xsi:type="string" translate="true">Grand Total (Base)</item>
        </item>
    </argument>
</column>

Basically all I need is set my one class instead of Magento\Sales\Ui\Component\Listing\Column\Price. Any idea how to do that?

Best Answer

So here what I did:

added to my di.xml

<preference for="Magento\Sales\Ui\Component\Listing\Column\Price"
            type="Vendor\Module\Ui\Component\Listing\Columns\Price" />

And in that Price.php file I override prepareDataSource function like this

public function prepareDataSource(array $dataSource)
{
    if (isset($dataSource['data']['items'])) {
        foreach ($dataSource['data']['items'] as & $item) {
            $currencyCode = isset($item['base_currency_code']) ? $item['base_currency_code'] : null;
            if ($this->getData('name')=='base_grand_total') {
               $item[$this->getData('name')] += //whatever I want to add;
            }

            $item[$this->getData('name')] = $this->priceFormatter->format(
                $item[$this->getData('name')],
                false,
                null,
                null,
                $currencyCode
            );
        }
    }

    return $dataSource;
}

I am not sure if this is the best way, but it worked