Magento 2 Pricing – Rounding Product Price

magento2priceproductrounding

Because of business logic I need to round all product prices to the nearest thousand.

Example:

If i have a price of $18600 i need to change the price to $19000. If i have $18400 i need to change it to $18000.

I've played with setDisplayValue($value) and getDisplayValue() methods from Magento_Framework/Pricing/Render/Amount.php but this does not change the price in all the store.

What file(s) should i modify to affect the behavior in all the store?

Thanks,

A. Martz.

Best Answer

I have modified a method convert of file Magento\Directory\Model\Currency.

declaring a plugin, file Vendor/Module/etc/di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd"> 
<type name="Magento\Directory\Model\Currency">
    <plugin name="vendor_module" type="Vendor\Module\Model\Plugin\Currency" />
</type>  

plugin model, file Vendor/Module/Model/Plugin/Currency.php

<?php
namespace Vendor\Module\Model\Plugin;
use Magento\Framework\Exception\InputException;

class Currency
{
    public function aroundConvert($subject, $proceed, $price, $toCurrency = null)
    {
        $price = $proceed($price, $toCurrency);     
        // you logic
        // warning ... logic affects the price of shipping  
        return $price;
    }
}

Also see Complete code on GitHub

Related Topic