I'm trying to find out if there is any way to remove decimals from layered navigation. We don't have product prices with decimals but Magento show 0-99.99,100-199.99 etc. How this can be achieved?
3 Answers
Your goal could be achieved in the next way:
app/code/YourNamespace/YourModule/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\Framework\Pricing\PriceCurrencyInterface"
type="YourNamespace\YourModule\Model\PriceCurrency" />
</config>
app/code/YourNamespace/YourModule/Model/PriceCurrency.php
<?php
namespace YourNamespace\YourModule\Model;
use Magento\Framework\Pricing\PriceCurrencyInterface;
class PriceCurrency extends \Magento\Directory\Model\PriceCurrency implements PriceCurrencyInterface
{
/**
* @inheritdoc
*/
const PRECISION_ZERO = 0;
/**
* {@inheritdoc}
*/
public function format(
$amount,
$includeContainer = true,
$precision = self::PRECISION_ZERO,
$scope = null,
$currency = null
) {
return $this->getCurrency($scope, $currency)
->formatPrecision($amount, $precision, [], $includeContainer);
}
}
The rewritten method is used not only for layered navigation. Please test if nothing is broken on frontend before releasing it on live server.
-
Thanks, im trying to look for something which only works for layered navigation. I found this extension github.com/lillik/magento2-price-decimal but its removing decimals from all places. Checking the possibility of customizing it.– BharathFeb 9, 2019 at 13:45
One extension is there with the help of it, you can achieve the rounding.
It will surely help you in some ways.
https://github.com/karliuka/m2.Price
This module deals with the price rounding management in Magento 2.
you can use round() php function. eg
$price = 129.56
echo round($price);
it will display 130.