7

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?

  • are you want to skip currency precision to 0 decimal instead of 2? – Rakesh Jesadiya Dec 4 '17 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 Dec 4 '17 at 9:58
6
+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.

  • In REST calls decimal part still presented ... "base_discount_amount": -48749.61, "base_grand_total": 76249.39, ... – Dmitry Dubovitsky Feb 5 '18 at 15:33
  • Not only that but the grand total of the cart is also computed on un-rounded values. – nicolallias Jun 28 '18 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) – nicolallias Jun 28 '18 at 12:14
4

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

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.