Hello Magento Friends,
Facing the issue of currency symbol not being displayed on the product, cart, and checkout page in Magento 2? I am here to provide a solution to this problem.
For a global reach of customers, it is necessary to display product prices on the basis of customers’ locality. A Magento store that supports different types of currencies can enhance the shopping experience of its customers. Automatically switch currency of the Magento 2 store based on the user’s location and provide flexibility of payment.
Learn How to Configure Currency in Magento 2
You have configured currency properly but sometimes you face issues with currency symbols. After you upgrade to Magento 2.4.3, it may happen that the currency symbol is not displayed on the product page, cart page, or checkout page. Let’s solve this issue
Contents
Steps to Solve Currency Symbol not Showing on Product, Cart and Checkout Page in Magento 2:
Step 1: First, we need to create a “di.xml” file inside our extension at the following path.
app\code\Vendor\Extension\etc\
Then add the code as follows
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\Directory\Model\Currency" type="Vendor\Extension\Model\Currency" /> </config> |
Step 2: After that, we need to create a “Currency.php” file inside the below path folder to add a file in the module.
app\code\Vendor\Extension\Model\
Then add the code as mentioned underneath
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 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
<?php namespace Vendor\Extension\Model; use Magento\Framework\App\ObjectManager; use Magento\Framework\Exception\InputException; use Magento\Directory\Model\Currency\Filter; use Magento\Framework\Locale\Currency as LocaleCurrency; use Magento\Framework\Locale\ResolverInterface as LocalResolverInterface; use Magento\Framework\NumberFormatterFactory; use Magento\Framework\Serialize\Serializer\Json; class Currency extends \Magento\Directory\Model\Currency { protected function _construct() { $this->_init(\Magento\Directory\Model\ResourceModel\Currency::class); } public function getCode() { return $this->_getData('currency_code'); } public function getCurrencyCode() { return $this->_getData('currency_code'); } public function getRates() { return $this->_rates; } public function setRates(array $rates) { $this->_rates = $rates; return $this; } public function load($id, $field = null) { $this->unsRate(); $this->setData('currency_code', $id); return $this; } public function getRate($toCurrency) { $code = $this->getCurrencyCodeFromToCurrency($toCurrency); $rates = $this->getRates(); if (!isset($rates[$code])) { $rates[$code] = $this->_getResource()->getRate($this->getCode(), $toCurrency); $this->setRates($rates); } return $rates[$code]; } public function getAnyRate($toCurrency) { $code = $this->getCurrencyCodeFromToCurrency($toCurrency); $rates = $this->getRates(); if (!isset($rates[$code])) { $rates[$code] = $this->_getResource()->getAnyRate($this->getCode(), $toCurrency); $this->setRates($rates); } return $rates[$code]; } public function convert($price, $toCurrency = null) { if ($toCurrency === null) { return $price; } elseif ($rate = $this->getRate($toCurrency)) { return (float)$price * (float)$rate; } throw new \Exception(__( 'Undefined rate from "%1-%2".', $this->getCode(), $this->getCurrencyCodeFromToCurrency($toCurrency) )); } private function getCurrencyCodeFromToCurrency($toCurrency) { if (is_string($toCurrency)) { $code = $toCurrency; } elseif ($toCurrency instanceof \Magento\Directory\Model\Currency) { $code = $toCurrency->getCurrencyCode(); } else { throw new InputException(__('Please correct the target currency.')); } return $code; } public function getFilter() { if (!$this->_filter) { $this->_filter = $this->_currencyFilterFactory->create(['code' => $this->getCode()]); } return $this->_filter; } public function format($price, $options = [], $includeContainer = true, $addBrackets = false) { return $this->formatPrecision($price, 2, $options, $includeContainer, $addBrackets); } public function formatPrecision( $price, $precision, $options = [], $includeContainer = true, $addBrackets = false ) { if (!isset($options['precision'])) { $options['precision'] = $precision; } if ($includeContainer) { return '<span class="price">' . ($addBrackets ? '[' : '') . $this->formatTxt( $price, $options ) . ($addBrackets ? ']' : '') . '</span>'; } return $this->formatTxt($price, $options); } public function formatTxt($price, $options = []) { if (!is_numeric($price)) { $price = $this->_localeFormat->getNumber($price); } $price = sprintf("%F", $price); /* if ($this->canUseNumberFormatter($options)) { return $this->formatCurrency($price, $options); }*/ return $this->_localeCurrency->getCurrency($this->getCode())->toCurrency($price, $options); } private function canUseNumberFormatter(array $options): bool { $allowedOptions = [ 'precision', LocaleCurrency::CURRENCY_OPTION_DISPLAY, LocaleCurrency::CURRENCY_OPTION_SYMBOL ]; if (!empty(array_diff(array_keys($options), $allowedOptions))) { return false; } if (array_key_exists('display', $options) && $options['display'] !== \Magento\Framework\Currency::NO_SYMBOL && $options['display'] !== \Magento\Framework\Currency::USE_SYMBOL ) { return false; } return true; } private function formatCurrency(string $price, array $options): string { $customerOptions = new \Magento\Framework\DataObject([]); $this->_eventManager->dispatch( 'currency_display_options_forming', ['currency_options' => $customerOptions, 'base_code' => $this->getCode()] ); $options += $customerOptions->toArray(); $this->numberFormatter = $this->getNumberFormatter($options); $formattedCurrency = $this->numberFormatter->formatCurrency( $price, $this->getCode() ?? $this->numberFormatter->getTextAttribute(\NumberFormatter::CURRENCY_CODE) ); if (array_key_exists(LocaleCurrency::CURRENCY_OPTION_SYMBOL, $options)) { // remove only one non-breaking space from custom currency symbol to allow custom NBSP in currency symbol $formattedCurrency = preg_replace('/ /u', '', $formattedCurrency, 1); } if ((array_key_exists(LocaleCurrency::CURRENCY_OPTION_DISPLAY, $options) && $options[LocaleCurrency::CURRENCY_OPTION_DISPLAY] === \Magento\Framework\Currency::NO_SYMBOL)) { $formattedCurrency = str_replace(' ', '', $formattedCurrency); } return preg_replace('/^\s+|\s+$/u', '', $formattedCurrency); } private function getNumberFormatter(array $options): \Magento\Framework\NumberFormatter { $key = 'currency_' . md5($this->localeResolver->getLocale() . $this->serializer->serialize($options)); if (!isset($this->numberFormatterCache[$key])) { $this->numberFormatter = $this->numberFormatterFactory->create( ['locale' => $this->localeResolver->getLocale(), 'style' => \NumberFormatter::CURRENCY] ); $this->setOptions($options); $this->numberFormatterCache[$key] = $this->numberFormatter; } return $this->numberFormatterCache[$key]; } private function setOptions(array $options): void { if (array_key_exists(LocaleCurrency::CURRENCY_OPTION_SYMBOL, $options)) { $this->numberFormatter->setSymbol( \NumberFormatter::CURRENCY_SYMBOL, $options[LocaleCurrency::CURRENCY_OPTION_SYMBOL] ); } if (array_key_exists(LocaleCurrency::CURRENCY_OPTION_DISPLAY, $options) && $options[LocaleCurrency::CURRENCY_OPTION_DISPLAY] === \Magento\Framework\Currency::NO_SYMBOL) { $this->numberFormatter->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, ''); } if (array_key_exists('precision', $options)) { $this->numberFormatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $options['precision']); } } public function getCurrencySymbol() { return $this->_localeCurrency->getCurrency($this->getCode())->getSymbol(); } public function getOutputFormat() { $formatted = $this->formatTxt(0); $number = $this->formatTxt(0, ['display' => \Magento\Framework\Currency::NO_SYMBOL]); return str_replace($this->trimUnicodeDirectionMark($number), '%s', $formatted); } public function getCurrencyRates($currency, $toCurrencies = null) { if ($currency instanceof \Magento\Directory\Model\Currency) { $currency = $currency->getCode(); } $data = $this->_getResource()->getCurrencyRates($currency, $toCurrencies); return $data; } public function saveRates($rates) { $this->_getResource()->saveRates($rates); return $this; } private function trimUnicodeDirectionMark($string) { if (preg_match('/^(\x{200E}|\x{200F})/u', $string, $match)) { $string = preg_replace('/^'.$match[1].'/u', '', $string); } return $string; } } |
Conclusion:
Hopefully, everyone was able to resolve the issue of Currency Symbol not Showing on the Product, Cart, and Checkout Page in Magento 2. If still, the problem persists, I can help you with it. You can catch me through the comment section.
Change the position of the currency symbol as in before the amount or after the amount in Magento 2 – Click Here
If you found the solution useful, share it with your friends and hit 5 stars. Stay in the loop with us for more Magento 2 solutions.
Happy Coding!