Magento2 – How to Remove Decimal from Quantity

admingridmagento2productquantity

How to remove decimal from quantity on Catalog Admin grid of Magento2 (Product/Catalog).

I have set Qty Uses Decimals to "No", but the decimals are still there.

enter image description here

Best Answer

This decimal comes from database. So if you need to remove this, then you need to create column renderer. Here is an example

In your etc/module.xml file add sequence tag follows:


<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_CatalogInventory"/>
        </sequence>
    </module>
</config>

[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" class="Magento\Catalog\Ui\Component\Listing\Columns">
        <column name="qty" class="[Vendor]\[Module]\Ui\Component\Listing\Columns\Quantity">
            <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>

Create a renderer [Vendor][Module]/Ui/Component/Listing/Columns/Quantity.php


namespace [Vendor]\[Module]\Ui\Component\Listing\Columns;

class Quantity extends \Magento\Ui\Component\Listing\Columns\Column
{
    /**
     * Column name
     */
    const NAME = 'column.qty';

    /**
     * Prepare Data Source
     *
     * @param array $dataSource
     * @return array
     */
    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;
    }
}

Create a renderer [Vendor][Module]/registration.php


\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Vendor_Module',
    __DIR__
);

Related Topic