Magento 2 Admin Price Inputs Rounding to 2 Decimals – Fix

adminhtmlcurrencymagento2.3roundinguicomponent

This is a Magento CE 2.3.2 project

In this project we need to work with 4 decimals for prices. We have tried this https://github.com/lillik/magento2-price-decimal which works fine, all prices are printed with 4 decimals in frontend, calculations are fine, and values are registered with 4 decimals in database tables when updating data through import/export functionality, or saving a product in admin, if we enter 4 decimal values

We have only 1 problem, but a big problem. In admin, in product edit page the html price input's are showing the value rounded to 2 decimals. So if we have a product with a price 1000.9555 €, if some admin user changes just one single letter in product's description then the rounded price value which is shown in the input (1000.96) is sent to form & saved, overwriting it to 1000.9600 in database

We have spent some days trying to find which template / js is responsible to print that form element, with no success. Any tips?

Best Answer

You may try the step below for this customization:

I assume you are using a custom module name "Company_MyModle"

step 1)

Create a di.xml file under MAGETNTO-ROOT/app/code/Company/Company_MyModle/etc/adminhtml

File : di.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">   
     <virtualType name="Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\Pool" type="Magento\Ui\DataProvider\Modifier\Pool">
        <arguments>
            <argument name="modifiers" xsi:type="array">
                <item name="pricetab" xsi:type="array">
                    <item name="class" xsi:type="string">Company\MyModule\Ui\DataProvider\Product\Modifier\Price</item>
                    <item name="sortOrder" xsi:type="number">200</item>
                </item>
            </argument>
        </arguments>
    </virtualType>    
</config>

step 2)

Create the file Price.php under MAGETNTO-ROOT/app/code/Company/MyModle/Ui/DataProvider/Product/Modifier

File : Price.php

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Company\MyModle\Ui\DataProvider\Product\Modifier;
use Magento\Catalog\Ui\DataProvider\Product\Form\Modifier\AbstractModifier;
use Magento\Catalog\Api\Data\ProductAttributeInterface;
use Magento\Catalog\Model\Locator\LocatorInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\UrlInterface;
use Magento\Ui\Component\Form\Field;
/**
 * Class Eav
 *
 * @api
 *
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 * @SuppressWarnings(PHPMD.TooManyFields)
 * @SuppressWarnings(PHPMD.ExcessiveClassComplexity)
 * @since 101.0.0
 */
class Price extends AbstractModifier
{


     public function __construct(
        LocatorInterface $locator        
    ) {
        $this->locator = $locator;        
    }


     public function modifyData(array $data)
    {
        if (!$this->locator->getProduct()->getId() && $this->dataPersistor->get('catalog_product')) {
            return $this->resolvePersistentData($data);
        }
        $productId = $this->locator->getProduct()->getId();
        $productPrice =  $this->locator->getProduct()->getPrice();
        $data[$productId][self::DATA_SOURCE_DEFAULT]['price'] = number_format((float)$productPrice, 3, '.', ''); 
        return $data;
    }



    public function modifyMeta(array $meta)
    {    
        return $meta;
    }


}

step 3) Run di.compile

Related Topic