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?

Rakesh Jesadiya
  • 42,221
  • 18
  • 132
  • 183
wingski
  • 111
  • 1
  • 3
  • are you want to skip currency precision to 0 decimal instead of 2? – Rakesh Jesadiya Dec 04 '17 at 09: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 04 '17 at 09:58

2 Answers2

11

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.

Rakesh Jesadiya
  • 42,221
  • 18
  • 132
  • 183
  • In REST calls decimal part still presented ... "base_discount_amount": -48749.61, "base_grand_total": 76249.39, ... – Dmitry Dubovitsky Feb 05 '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 09:18
  • I endend up to a pretty neat module https://github.com/karliuka/m2.Price which have been through all these problems (and some others) – nicolallias Jun 28 '18 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 . – Abilash Erikson Sep 06 '20 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

Khoa TruongDinh
  • 32,054
  • 11
  • 88
  • 155
  • 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 . – Abilash Erikson Sep 06 '20 at 10:14
  • @abilasher I think you should check this module: https://github.com/Magento-Japan/m2-jplocalize/tree/master/price – Khoa TruongDinh Sep 06 '20 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 . – Abilash Erikson Sep 06 '20 at 10:22