Merge pull request #629 from jitendra-webkul/jitendra

convertToBasePrice function added to Core class
This commit is contained in:
JItendra Singh 2019-02-26 19:09:33 +05:30 committed by GitHub
commit 3fd2a5423e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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
*