11

In \magento\framework\Pricing\PriceCurrencyInterface, I want to change the constant in that php interface file from this:

/**
 * Default precision
 */
const DEFAULT_PRECISION = 2;

to this:

/**
 * Default precision
 */
const DEFAULT_PRECISION = 0;

What is the best practices to override this php interface from the magento core files?

2
  • are you want to skip currency precision to 0 decimal instead of 2? Commented Dec 4, 2017 at 9:30
  • Yes. It's happening all over the modules, in some part of the pages the decimal places in price still happening. So instead of overriding or extending each files in respective modules that used PriceCurrencyInterface, i think overriding the php interface that declare the constant would be a better solution.
    – wingski
    Commented Dec 4, 2017 at 9:58

2 Answers 2

11
+50

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

You can set precision of price using below way,

Just need to create a simple module for it,

Create Rbj/PriceCurrency module under the app/code folder Magento.

Create registration.php file,

<?php

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

create etc folder,

app/code/Rbj/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="Rbj_PriceCurrency" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Backend"/>
            <module name="Magento_Directory"/>
        </sequence>
    </module>
</config>

app/code/Rbj/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="Rbj\PriceCurrency\Model\Directory\Currency" />
</config>

app/code/Rbj/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="Rbj\PriceCurrency\Plugin\FormatPrice" sortOrder="10" />
    </type>
</config>

Create model file which we have decalred in di.xml file,

app/code/Rbj/PriceCurrency/Model/Directory/Currency.php

<?php
namespace Rbj\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);
    }
}

Create app/code/Rbj/PriceCurrency/Plugin/FormatPrice.php

<?php
namespace Rbj\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'] = 0;

        return $returnValue;
    }
}

Run php bin/magento setup:upgrade command.

4
  • In REST calls decimal part still presented ... "base_discount_amount": -48749.61, "base_grand_total": 76249.39, ... Commented Feb 5, 2018 at 15:33
  • Not only that but the grand total of the cart is also computed on un-rounded values. Commented Jun 28, 2018 at 9:18
  • I endend up to a pretty neat module github.com/karliuka/m2.Price which have been through all these problems (and some others) Commented Jun 28, 2018 at 12:14
  • is there any way i can do without installing any module. i can edit the code in any file . But ia m really worry to install any module it will end up with breaking my website . Commented Sep 6, 2020 at 10:10
6

First of all, I will answer your question

What is the best practices to override this php interface from the magento core files?

From the basic knowledge, we cannot override an Interface. In Magento, it would be the same.

Second, take a look at vendor/magento/module-catalog/etc/di.xml

<preference for="Magento\Framework\Pricing\PriceCurrencyInterface" 
           type="Magento\Directory\Model\PriceCurrency" />

As we can see, the actual class which we need to override is Magento\Directory\Model\PriceCurrency.

Third, this module may be your need: https://github.com/Magento-Japan/m2-jplocalize/tree/master/price

3
  • in vendor/magento/framework/pricing/amount/AmountFactory.php i added ceil function . as the result now product price is shown as i desired . But in cart and other pages still decimal is coming . I really worried to add a module to my website, because at the edn it will broke my website . Commented Sep 6, 2020 at 10:14
  • @abilasher I think you should check this module: github.com/Magento-Japan/m2-jplocalize/tree/master/price Commented Sep 6, 2020 at 10:17
  • really thank you for your reply . I really don't want to install any module . That's why i am looking for the code . Also do you know how can i show currency based on the location ? I have already posted this question in my stackoverflow main account and add a bounty for that . May be you can help . please check . Commented Sep 6, 2020 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.