Question

I want to remove decimal points from special price column in product admin grid.

Was it helpful?

Solution

First of all, you will need to create a simple module, For this you need to add app/code/Vendor/Module/etc/module.xml file for adding sequence.

<?xml version="1.0" encoding="UTF-8"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
   <module name="Vendor_Module" setup_version="1.0.0">
      <sequence>
         <module name="Magento_Catalog" />
      </sequence>
   </module>
</config>

The next step is to create a product_listing.xml file in app/code/Vendor/Module/Module/view/adminhtml/ui_component/ directory and copy the following code.

<?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" class="Magento\Catalog\Ui\Component\Listing\Columns">
      <column name="special_price" class="Vendor\Module\Ui\Component\Listing\Columns\DecimalRemove">
         <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">Special Price</item>
               <item name="sortOrder" xsi:type="number">75</item>
            </item>
         </argument>
      </column>
   </columns>
</listing>

After that, you will have to add a DecimalRemove.php renderer file in app/code/Vendor/Module/Ui/Component/Listing/Columns/ directory and copy the following code.

<?php

namespace Vendor\Module\Ui\Component\Listing\Columns;
class DecimalRemove extends \Magento\Ui\Component\Listing\Columns\Column
{
    public function prepareDataSource(array $dataSource)
    {
        if (isset($dataSource['data']['items']))
        {
            $fieldName = $this->getData('name');
            foreach ($dataSource['data']['items'] as & $item)
            {
                if (isset($item[$fieldName]))
                {
                    $item[$fieldName] = (int)$item[$fieldName];
                }
            }
        }
        return $dataSource;
    }
}

Lastly, you will have to upgrade your module and clean the cache.

php -dmemory_limit=1G bin/magento setup:upgrade
php -dmemory_limit=1G bin/magento setup:di:compile
php -dmemory_limit=1G bin/magento setup:static-content:deploy -f
php bin/magento cache:clean
php bin/magento cache:flush

I hope this will help

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top