7

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

3 Answers 3

12

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__
);

1
  • 1
    didn't work in 2.1 version, I added to an existing module, run di:compile and static content deploy Apr 7, 2017 at 13:33
1

In Sohel Rana's Answer also add registration.php under [Vendor][Module] as follows

 <?php

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

Incase you forgot.

Thanks. I was not able to add comment so I posted this seeing Gomma's comment

0

I have decimal removed over the databaseenter image description here. See the screenshot:

1
  • Editing database is never a good choice especially since you dont know what the future updates / plugins will do with those numbers. Sep 25, 2020 at 0:00

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.