Made Optional

This commit is contained in:
Devansh 2022-02-04 12:34:33 +05:30
parent 96d084488d
commit 851637ac11
1 changed files with 33 additions and 18 deletions

View File

@ -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.