6

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

10

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

| improve this answer | |
  • 1
    didn't work in 2.1 version, I added to an existing module, run di:compile and static content deploy – Mohammed Gomma Apr 7 '17 at 13:33
1

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

| improve this answer | |
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

| improve this answer | |

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.