convertToBasePrice function added to Core class

This commit is contained in:
jitendra 2019-02-26 19:07:51 +05:30
parent 1ce5a957f9
commit 01f3cd7324
1 changed files with 28 additions and 2 deletions

View File

@ -319,7 +319,7 @@ class Core
/**
* Converts price
*
* @param float $price
* @param float $amount
* @param string $targetCurrencyCode
* @return string
*/
@ -336,12 +336,38 @@ class Core
'target_currency' => $targetCurrency->id,
]);
if (null === $exchangeRate)
if (null === $exchangeRate || ! $exchangeRate->rate)
return $amount;
return (float) $amount * $exchangeRate->rate;
}
/**
* Converts to base price
*
* @param float $amount
* @param string $targetCurrencyCode
* @return string
*/
public function convertToBasePrice($amount, $targetCurrencyCode = null)
{
$targetCurrency = !$targetCurrencyCode
? $this->getCurrentCurrency()
: $this->currencyRepository->findOneByField('code', $targetCurrencyCode);
if (! $targetCurrency)
return $amount;
$exchangeRate = $this->exchangeRateRepository->findOneWhere([
'target_currency' => $targetCurrency->id,
]);
if (null === $exchangeRate || ! $exchangeRate->rate)
return $amount;
return (float) $amount / $exchangeRate->rate;
}
/**
* Format and convert price with currency symbol
*