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
1 2 3 4 |
<?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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
<?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!
hello,
we followed the steps mentioned in your link but it didnt work can u let us know what are we missing here.