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 Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

21 hours ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

3 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

3 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

4 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

5 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago