How To

Magento 2: Remove Decimal From Quantity In Product Grid In Magento Admin

Hello Magento Pals, 

Last time we learned How to Write translatable strings in Email, PHTML, UiComponents, js, etc. in Magento 2

Today, we are going to learn how to remove decimals from the quantity product grid in Magento admin. First, If you want to remove quantity decimal, then you can do it via this code.

Create file app\code\VENDOR\EXTENSION\module.xml file

<module name="VENDOR_EXTENSION" setup_version="1.0.0">
    <sequence>
        <module name="Magento_CatalogInventory"/>
    </sequence>
</module>

Create app\code\VENDOR\EXTENSION\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\QtyFile">
            <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 app\code\VENDOR\EXTENSION\Ui\Component\Listing\Columns\QtyFile.php

<?php

namespace VENDOR\EXTENSION\Ui\Component\Listing\Columns;

class QtyFile extends \Magento\Ui\Component\Listing\Columns\Column
{
    /**
     * 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;
    }
}

and that’s it- Now, You can remove decimal from quantity in product grid in Magento admin. The code strings above are just for the reference only you can customize it and implement it for your use as per your needs. If you face any problem while during so then contact our support team.

Lastly, feel free to comment below and let us know what you think about this article. Also, you can share this with your Magento colleagues and partners.

Happy Coding folks ? & If you haven’t checked our #MageheartDays Sale for Magento Lovers, Check all lovely offers from MageComp here

 

 

Click to rate this post!
[Total: 4 Average: 4.8]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

Recent Posts

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

3 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

4 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

5 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

7 days ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago

Magento 2: How to Save Custom Field Value to quote_address for Multi-Shipping Orders

Hello Magento Friends, In Magento 2, the checkout process allows customers to choose multiple shipping…

1 week ago