2

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?

2
+100

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

  • Points for you. This worked like a charm :) I'll give you the points tomorrow (system don't allow to give them during the 1st day). Just one thing: after applying your code we have found a new issue: TierPrice renderer still show 2 decimals. Let me ask your opinion... could we the follow same approach rewriting the existing modifier in vendor/magento/module-catalog/Ui/DataProvider/Product/Form/Modifier/TierPrice.php, right? – Raul Sanchez Jul 15 '19 at 10:11
  • Easier than that, as modifyData is returning the whole product data in $data array we can manipulate there every field we need, not only price – Raul Sanchez Jul 15 '19 at 13:02
  • Yes. You can also go with modifying the whole modifyData to take control of everything. Thank you. – Pritam Info 24 Jul 16 '19 at 6:28
  • Anybody got this working with magento 2.3? I tried above but nothing changes and the field is still rounded to 2 digits – Sander van Zuidam Sep 23 '19 at 9:41
0

need to override dependency file of priceCurrency interface by below way to change precision of price.

you can set precision of price using custom module.

create custom module with below step.

step2 : create registration.php file,

<?php

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

step2 : app/code/Adamsmage/PriceCurrency/etc/module.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Adamsmage_PriceCurrency" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Backend"/>
            <module name="Magento_Directory"/>
        </sequence>
    </module>
</config>

step3: app/code/Adamsmage/PriceCurrency/etc/di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Directory\Model\Currency" type="Adamsmage\PriceCurrency\Model\Directory\Currency" />
</config>

step4: app/code/Adamsmage/PriceCurrency/etc/frontend/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Framework\Locale\Format">
        <plugin name="format-price" type="Adamsmage\PriceCurrency\Plugin\FormatPrice" sortOrder="10" />
    </type>
</config>

step5 : app/code/Adamsmage/PriceCurrency/Model/Directory/Currency.php

<?php
namespace Adamsmage\PriceCurrency\Model\Directory;

class Currency extends \Magento\Directory\Model\Currency
{
    /*
    * You can set precision from here in $options array
    */
    public function formatTxt($price, $options = [])
    {
        if (!is_numeric($price)) {
            $price = $this->_localeFormat->getNumber($price);
        }
        $price = sprintf("%F", $price);

        $options['precision'] = 0;

        return $this->_localeCurrency->getCurrency($this->getCode())->toCurrency($price, $options);
    }
}

step6 : app/code/Adamsmage/PriceCurrency/Plugin/FormatPrice.php

<?php
namespace Adamsmage\PriceCurrency\Plugin;

class FormatPrice
{
    /*
    * Returns an array with price formatting info
    *
    * \Magento\Framework\Locale\Format $subject
    */
    public function aroundGetPriceFormat(\Magento\Framework\Locale\Format $subject, callable $proceed, $localeCode = null, $currencyCode = null)
    {
        $returnValue = $proceed($localeCode, $currencyCode);

        $returnValue['requiredPrecision'] = 4;

        return $returnValue;
    }
}

Than Run below commands.

php bin/magento setup:upgrade

php bin/magento setup:di:compile

php bin/magento setup:static-content:deploy

i hope its work for you.

  • I think you have misunderstood the question... It seems your proposal does the same as github.com/lillik/magento2-price-decimal does. But the question wasn't this, we asked about the issue with the admin renderer for price input – Raul Sanchez Jul 15 '19 at 10:07

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.