From 2c4fa0771676b562f84de41069d64aff648172ba Mon Sep 17 00:00:00 2001 From: nmalevanec Date: Wed, 13 Dec 2017 16:11:41 +0200 Subject: [PATCH 1/2] 6965: magento/magento2#6965: \Magento\Directory\Model\PriceCurrency::format() fails without conversion rate --- .../Magento/Directory/Model/PriceCurrency.php | 11 ++++- .../Directory/Model/PriceCurrencyTest.php | 43 +++++++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 dev/tests/integration/testsuite/Magento/Directory/Model/PriceCurrencyTest.php diff --git a/app/code/Magento/Directory/Model/PriceCurrency.php b/app/code/Magento/Directory/Model/PriceCurrency.php index a211242d377f3..c0c8e949ea4da 100644 --- a/app/code/Magento/Directory/Model/PriceCurrency.php +++ b/app/code/Magento/Directory/Model/PriceCurrency.php @@ -77,8 +77,15 @@ public function format( $scope = null, $currency = null ) { - return $this->getCurrency($scope, $currency) - ->formatPrecision($amount, $precision, [], $includeContainer); + if ($currency instanceof Currency) { + $currentCurrency = $currency; + } elseif (is_string($currency)) { + $currentCurrency = $this->currencyFactory->create()->load($currency); + } else { + $currentCurrency = $this->getStore($scope)->getCurrentCurrency(); + } + + return $currentCurrency->formatPrecision($amount, $precision, [], $includeContainer); } /** diff --git a/dev/tests/integration/testsuite/Magento/Directory/Model/PriceCurrencyTest.php b/dev/tests/integration/testsuite/Magento/Directory/Model/PriceCurrencyTest.php new file mode 100644 index 0000000000000..fd6b8577eaf98 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Directory/Model/PriceCurrencyTest.php @@ -0,0 +1,43 @@ +priceCurrency = Bootstrap::getObjectManager()->get(PriceCurrency::class); + } + + /** + * Check PriceCurrency::format() doesn't depend on currency rate configuration. + * @return void + */ + public function testFormat() + { + self::assertSame( + 'AFN10.00', + $this->priceCurrency->format(10, true, 2, null, 'AFN') + ); + } +} From ed5ffedd4cd28492f299e5d059414dd3d2540b8e Mon Sep 17 00:00:00 2001 From: nmalevanec Date: Thu, 14 Dec 2017 12:09:58 +0200 Subject: [PATCH 2/2] 6965: magento/magento2#6965: \Magento\Directory\Model\PriceCurrency::format() fails without conversion rate --- .../Magento/Directory/Model/PriceCurrency.php | 51 ++++++++++--------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/app/code/Magento/Directory/Model/PriceCurrency.php b/app/code/Magento/Directory/Model/PriceCurrency.php index c0c8e949ea4da..07d2e60d61335 100644 --- a/app/code/Magento/Directory/Model/PriceCurrency.php +++ b/app/code/Magento/Directory/Model/PriceCurrency.php @@ -77,15 +77,7 @@ public function format( $scope = null, $currency = null ) { - if ($currency instanceof Currency) { - $currentCurrency = $currency; - } elseif (is_string($currency)) { - $currentCurrency = $this->currencyFactory->create()->load($currency); - } else { - $currentCurrency = $this->getStore($scope)->getCurrentCurrency(); - } - - return $currentCurrency->formatPrecision($amount, $precision, [], $includeContainer); + return $this->createCurrency($scope, $currency)->formatPrecision($amount, $precision, [], $includeContainer); } /** @@ -108,20 +100,7 @@ public function convertAndFormat( */ public function getCurrency($scope = null, $currency = null) { - if ($currency instanceof Currency) { - $currentCurrency = $currency; - } elseif (is_string($currency)) { - $currency = $this->currencyFactory->create() - ->load($currency); - $baseCurrency = $this->getStore($scope) - ->getBaseCurrency(); - $currentCurrency = $baseCurrency->getRate($currency) ? $currency : $baseCurrency; - } else { - $currentCurrency = $this->getStore($scope) - ->getCurrentCurrency(); - } - - return $currentCurrency; + return $this->createCurrency($scope, $currency, true); } /** @@ -164,4 +143,30 @@ public function round($price) { return round($price, 2); } + + /** + * Get currency considering currency rate configuration. + * + * @param null|string|bool|int|\Magento\Framework\App\ScopeInterface $scope + * @param \Magento\Framework\Model\AbstractModel|string|null $currency + * @param bool $includeRate + * + * @return Currency + */ + private function createCurrency($scope, $currency, bool $includeRate = false) + { + if ($currency instanceof Currency) { + $currentCurrency = $currency; + } elseif (is_string($currency)) { + $currentCurrency = $this->currencyFactory->create()->load($currency); + if ($includeRate) { + $baseCurrency = $this->getStore($scope)->getBaseCurrency(); + $currentCurrency = $baseCurrency->getRate($currentCurrency) ? $currentCurrency : $baseCurrency; + } + } else { + $currentCurrency = $this->getStore($scope)->getCurrentCurrency(); + } + + return $currentCurrency; + } }