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

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

1 day ago

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

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

1 day ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

2 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

3 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

5 days ago

How Upcoming Cookie Changes Will Affect Your E-commerce Website?

The e-commerce world is constantly in flux. New tech and strategies emerge daily to help…

5 days ago