1

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?

  • check my answer, i did it directly with ui_component xml. – LucScu Mar 4 '18 at 11:04
1

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

  • 1
    on my own, I think creating a new module and overriding as you did is the right way to do this – DependencyHell May 10 '17 at 11:49
  • check my answer, i did it directly with ui_component xml. – LucScu Mar 4 '18 at 11:03
0

Maybe could be useful for next users, instead of using di.xml i did it with ui component, just add to your custom module \app\code\Vendor\Module\view\adminhtml\ui_component\product_listing.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="product_columns">
        <column name="price" class="Vendor\Module\Ui\Component\Listing\Columns\Price">
            <argument name="data" xsi:type="array">
                <item name="config" xsi:type="array">
                <item name="filter" xsi:type="string">textRange</item>
                <item name="add_field" xsi:type="boolean">true</item>
                <item name="label" xsi:type="string" translate="true">Price</item>
                <item name="sortOrder" xsi:type="number">70</item>
            </item>
            </argument>
        </column>
    </columns>
</listing>

it is the magento price column config taked from \vendor\magento\module-catalog\view\adminhtml\ui_component\product_listing.xml except column component class that is replaced with your custom class.

Now you could use your custom class (like accepted answer) \app\code\Vendor\Module\Ui\Component\Listing\Columns\Price.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.