3

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 3

1

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.

1
  • 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.
    – Bharath
    Feb 9, 2019 at 13:45
1

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.

-1

you can use round() php function. eg

$price = 129.56
echo round($price);

it will display 130.

1
  • Where should i use this function, which file and location, please?
    – Bharath
    Feb 8, 2019 at 13:50

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.