Magento, Magento 2

Magento 2 : Remove decimals from quantity in product grid

Magento 2 Remove decimals from quantity in product grid

In this tutorial, we will learn about how to remove decimals from quantity in product grid in Magento 2. In the product admin grid, there are by default quantity display with decimal points. Now, if the user wants to remove that decimals from quantity then, we need to customize to remove that. Magento doesn’t provide by default any configuration for that.

So, How to implement that programmatically to remove decimals from quantity. Let’s follow the below steps for that :

You may also like this :

1) First of all, Let’s assume that you created the module already. Now, you need to modify your module.xml file to add sequence :


<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
 * Created By : Rohan Hapani
 */
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
   <module name="RH_Helloworld" setup_version="1.0.0">
      <sequence>
         <module name="Magento_CatalogInventory" />
      </sequence>
   </module>
</config>

2) After that, Create product_listing.xml file at app/code/RH/Helloworld/Module/view/adminhtml/ui_component/  and paste the below code :


<?xml version="1.0" encoding="UTF-8"?>
<!--
/**
 * Created By : Rohan Hapani
 */
-->
<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="qty" class="RH\Helloworld\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">Quantity</item>
               <item name="sortOrder" xsi:type="number">75</item>
            </item>
         </argument>
      </column>
   </columns>
</listing>

3) Now, need to add renderer file DecimalRemove.php at app/code/RH/Helloworld/Ui/Component/Listing/Columns/ and paste the below code :


<?php
/**
 * Created By : Rohan Hapani
 */
namespace RH\Helloworld\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;
    }
}

In Last, need to upgrade module and clean cache :


php bin/magento s:up
php bin/magento c:c

Finally, Decimals remove from quantity successfully. Cheers 🙂

I hope this blog will helpful for easily understand how to remove decimals from quantity in product grid in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution 🙂

Tagged ,