From 01f3cd732413a402a4500e9312047506f062274a Mon Sep 17 00:00:00 2001 From: jitendra Date: Tue, 26 Feb 2019 19:07:51 +0530 Subject: [PATCH] convertToBasePrice function added to Core class --- packages/Webkul/Core/src/Core.php | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/packages/Webkul/Core/src/Core.php b/packages/Webkul/Core/src/Core.php index e19d3d6e6..d1943eed4 100755 --- a/packages/Webkul/Core/src/Core.php +++ b/packages/Webkul/Core/src/Core.php @@ -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 *