From 851637ac11087dfead2a06e532b42a34b515fc4f Mon Sep 17 00:00:00 2001 From: Devansh Date: Fri, 4 Feb 2022 12:34:33 +0530 Subject: [PATCH] Made Optional --- packages/Webkul/Core/src/Core.php | 51 ++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 18 deletions(-) diff --git a/packages/Webkul/Core/src/Core.php b/packages/Webkul/Core/src/Core.php index 1dfca2845..9ad916d78 100755 --- a/packages/Webkul/Core/src/Core.php +++ b/packages/Webkul/Core/src/Core.php @@ -662,7 +662,7 @@ class Core $amount = 0; } - return $this->formatPrice($this->convertPrice($amount), $this->getCurrentCurrency()->code); + return $this->formatPrice($this->convertPrice($amount)); } /** @@ -678,23 +678,6 @@ class Core return $formatter->getSymbol(\NumberFormatter::CURRENCY_SYMBOL); } - /** - * Format and convert price with currency symbol. - * - * @param float $price - * @return string - */ - public function formatPrice($price, $currencyCode) - { - if (is_null($price)) { - $price = 0; - } - - $formatter = new \NumberFormatter(app()->getLocale(), \NumberFormatter::CURRENCY); - - return $formatter->formatCurrency($price, $currencyCode); - } - /** * Format and convert price with currency symbol. * @@ -717,6 +700,38 @@ class Core ]; } + /** + * Format and convert price with currency symbol. + * + * @param float $price + * @param string (optional) $currencyCode + * @return string + */ + public function formatPrice($price, $currencyCode = null) + { + if (is_null($price)) { + $price = 0; + } + + $currency = $currencyCode + ? $this->getAllCurrencies()->where('code', $currencyCode)->first() + : $this->getCurrentCurrency(); + + $formatter = new \NumberFormatter(app()->getLocale(), \NumberFormatter::CURRENCY); + + if ($symbol = $currency->symbol) { + if ($this->currencySymbol($currency) == $symbol) { + return $formatter->formatCurrency($price, $currency->code); + } + + $formatter->setSymbol(\NumberFormatter::CURRENCY_SYMBOL, $symbol); + + return $formatter->format($this->convertPrice($price)); + } + + return $formatter->formatCurrency($price, $currency->code); + } + /** * Format price with base currency symbol. This method also give ability to encode * the base currency symbol and its optional.