How To

How to Round Off All 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!

Click to rate this post!
[Total: 6 Average: 4.2]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

View Comments

  • hello,

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

Recent Posts

Generating Thumbnails with Spatie Media Library in Laravel 11: A Step-by-Step Guide

Generating image thumbnails is a common requirement in web applications, especially when handling media-heavy content.…

18 hours ago

Enhancing Web Application Security with Laravel’s Built-In Features

In today’s digital landscape, web application security is paramount. As a powerful PHP framework, Laravel…

2 days ago

Magento 2 Extensions Digest October 2024 (New Release & Updates)

October was an exciting month for MageComp! From significant updates across our Magento 2 extension…

2 days ago

Improving Error Handling and Transition Management in Remix with useRouteError and useViewTransitionState

In modern web development, seamless navigation and state management are crucial for delivering a smooth…

1 week ago

Magento Open Source 2.4.8-Beta Release Notes

Magento Open Source 2.4.8 beta version released on October  8, 2024. The latest release of…

2 weeks ago

How to Create Catalog Price Rule in Magento 2 Programmatically?

Hello Magento Friends, Creating catalog price rules programmatically in Magento 2 can be a valuable…

2 weeks ago