I want to remove decimal points from special price column in product admin grid.
1 Answer
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
-
it works for me Thanks. But i change only this (int)$item[$fieldName]; to (float)$item[$fieldName]; to get 2 decimal point number. Commented Nov 12, 2020 at 9:26
-
ok fine but In description you said you wanted to remove decimal points so I gave solution accordingly which is
(int)$item[$fieldName]
. I changed the question title accordingly please don't change it, it may help others as well in future Commented Nov 12, 2020 at 12:26 -
oh I am sorry it was my fault. I wrote it wrong in description . i wanted to set 2 decimal point value. Commented Nov 12, 2020 at 12:30
-
Actually you solved your problem through my answer now please don't change the question description and title it may help others in future otherwise I have to change my answer accordingly so I am changing the question description and title it still a valid question don't worry Commented Nov 12, 2020 at 12:54