How to Round Off All Prices in Magento 2

How to Round Of All The Prices in Magento 2

Hello Magento Friends,

Welcome back to Magento “How To” Blog Tutorials by MageComp. Today we will learn about an important topic, How to Round Off All Prices in Magento 2.

Default Magento displays float prices on all the pages. Promote a better product pricing display by rounding off the floating prices of your Magento 2 Product Catalog. Rounding off product prices also saves admin time and effort. Follow the below-given steps to Round Off All Prices in Magento 2.

So, let’s get started

Steps to Round Off All Prices in Magento 2:

Step 1: First, create a “di.xml” file inside our extension at the below path.

app\code\Vendor\Extension\etc\di.xml

Now, add this code

<?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\Render\Amount" type="Vendor\Extension\Pricing\Render\Amount"/>
</config>

Step 2: After that, create an  “Amount.php” file inside the below folder path of extension.

app\code\Vendor\Extension\Pricing\Render\Amount.php

And, add the code as mentioned below

<?php
namespace Vendor\Extension\Pricing\Render;

use Magento\Framework\Pricing\Amount\AmountInterface;
use Magento\Framework\Pricing\SaleableInterface;
use Magento\Framework\Pricing\Price\PriceInterface;
use Magento\Framework\Pricing\PriceCurrencyInterface;
use Magento\Framework\View\Element\Template;


class Amount extends Template implements \Magento\Framework\Pricing\Render\AmountRenderInterface
{
   
    protected $saleableItem;
    protected $price;
    protected $adjustmentRenders;
    protected $priceCurrency;
    protected $rendererPool;
    protected $amount;
    protected $displayValue;
    protected $adjustmentsHtml = [];
   
    public function __construct(
        Template\Context $context,
        AmountInterface $amount,
        PriceCurrencyInterface $priceCurrency,
        \Magento\Framework\Pricing\Render\RendererPool $rendererPool,
        SaleableInterface $saleableItem = null,
        PriceInterface $price = null,
        array $data = [])
    {
        parent::__construct($context, $data);
        $this->amount = $amount;
        $this->saleableItem = $saleableItem;
        $this->price = $price;
        $this->priceCurrency = $priceCurrency;
        $this->rendererPool = $rendererPool;
    }

    public function setDisplayValue($value)
    {
        $this->displayValue = $value;
    }
    public function getDisplayValue()
    {
        if ($this->displayValue !== null)
        {
            return round($this->displayValue);
        }
        else
        {
            return round($this->getAmount()->getValue());
        }
        
    }
    public function getAmount()
    {
        return $this->amount;
    }
    public function getSaleableItem()
    {
        return $this->saleableItem;
    }
    public function getPrice()
    {
        return $this->price;
    }
    public function getDisplayCurrencyCode()
    {
        return $this->priceCurrency->getCurrency()->getCurrencyCode();
    }
    public function getDisplayCurrencySymbol()
    {
        return $this->priceCurrency->getCurrencySymbol();
    }
    public function hasAdjustmentsHtml()
    {
        return (bool) count($this->adjustmentsHtml);
    }
    public function getAdjustmentsHtml()
    {
        return implode('', $this->adjustmentsHtml);
    }
    protected function _toHtml()
    {
        $adjustmentRenders = $this->getAdjustmentRenders();
        if ($adjustmentRenders)
        {
            $adjustmentHtml = $this->getAdjustments($adjustmentRenders);
            if (!$this->hasSkipAdjustments() ||
                ($this->hasSkipAdjustments() && $this->getSkipAdjustments() == false))
            {
                $this->adjustmentsHtml = $adjustmentHtml;
            }
        }
        $html = parent::_toHtml();
        return $html;
    }
    protected function getAdjustmentRenders()
    {
        return $this->rendererPool->getAdjustmentRenders($this->saleableItem, $this->price);
    }
    protected function getAdjustments($adjustmentRenders)
    {
        $this->setAdjustmentCssClasses($adjustmentRenders);
        $data = $this->getData();
        $adjustments = [];
        foreach ($adjustmentRenders as $adjustmentRender)
        {
            if ($this->getAmount()->getAdjustmentAmount($adjustmentRender->getAdjustmentCode()) !== false)
            {
                $html = $adjustmentRender->render($this, $data);
                if (trim($html))
                {
                    $adjustments[$adjustmentRender->getAdjustmentCode()] = $html;
                }
            }
        }
        return $adjustments;
    }
    public function formatCurrency(
        $amount,
        $includeContainer = true,
        $precision = PriceCurrencyInterface::DEFAULT_PRECISION)
    {
        return $this->priceCurrency->format($amount, $includeContainer, $precision);
    }
    protected function setAdjustmentCssClasses($adjustmentRenders)
    {
        $cssClasses = $this->hasData('css_classes') ? explode(' ', $this->getData('css_classes')) : [];
        $cssClasses = array_merge($cssClasses, array_keys($adjustmentRenders));
        $this->setData('adjustment_css_classes', join(' ', $cssClasses));
        return $this;
    }
}

That’s it!

Conclusion:

This way you are able to Round Off All Prices in Magento 2. If you face any difficulty and need expert help, reach our Certified Magento Developers to help you out with this. Stay in touch with us for more such solutions.

Happy Coding!

Previous Article

Seamlessly Integrating HDFC Payment Gateway into Magento 2: A Step-by-Step Guide

Next Article

How to Add Customized Size Charts in Magento 2?

Write a Comment
  1. hello,

    we followed the steps mentioned in your link but it didnt work can u let us know what are we missing here.

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨