From 0e38e350164154e35d3acd71f77c7142c45898a1 Mon Sep 17 00:00:00 2001 From: Luke Holder Date: Fri, 3 Apr 2020 09:36:01 +0800 Subject: [PATCH 1/3] Round before formatting --- src/web/twig/Extension.php | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/web/twig/Extension.php b/src/web/twig/Extension.php index 20bb02a728..40a67e1725 100644 --- a/src/web/twig/Extension.php +++ b/src/web/twig/Extension.php @@ -9,6 +9,7 @@ use Craft; use craft\commerce\errors\CurrencyException; +use craft\commerce\helpers\Currency; use craft\commerce\Plugin; use Twig\Error\Error; use Twig\Extension\AbstractExtension; @@ -54,20 +55,27 @@ public function getFilters(): array */ public function commerceCurrency($amount, $currency, $convert = false, $format = true, $stripZeros = false): string { - $this->_validatePaymentCurrency($currency); - // return input if no currency passed, and both convert and format are false. if (!$convert && !$format) { return $amount; } + if ($convert) { + $this->_validatePaymentCurrency($currency); // must be a payment currency to convert + } + if ($convert) { $amount = Plugin::getInstance()->getPaymentCurrencies()->convert($amount, $currency); } if ($format) { - $options[\NumberFormatter::ROUNDING_MODE] = \NumberFormatter::ROUND_HALFUP; - $amount = Craft::$app->getFormatter()->asCurrency($amount, $currency, $options, [], $stripZeros); + + // Round it before formatting + if ($currencyData = Plugin::getInstance()->getCurrencies()->getCurrencyByIso($currency)) { + $amount = Currency::round($amount, $currencyData); // Will round to the right minorUnits + } + + $amount = Craft::$app->getFormatter()->asCurrency($amount, $currency, [], [], $stripZeros); } return $amount; From 73a4c047a2714e2e3c36fed85568ce7ab223794b Mon Sep 17 00:00:00 2001 From: Luke Holder Date: Fri, 3 Apr 2020 09:40:18 +0800 Subject: [PATCH 2/3] Fixed #1353 --- CHANGELOG.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 451a238801..a5ee20a939 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # Release Notes for Craft Commerce +## Unreleased + +### Fixed +- Fixed an error that could occur when using the `|commerceCurrency` Twig filter if the Intl extension wasn’t enabled. ([#1353])(https://github.com/craftcms/commerce/issues/1353)) + ## 3.1.0.1 - 2020-04-02 ### Fixed From 90e69e1c00979660b32ca9e98a9e6aafd3fa5b55 Mon Sep 17 00:00:00 2001 From: Luke Holder Date: Fri, 3 Apr 2020 15:09:09 +0800 Subject: [PATCH 3/3] Finish 3.1.0.2 --- CHANGELOG.md | 2 +- composer.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a5ee20a939..19cd5badc5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,6 @@ # Release Notes for Craft Commerce -## Unreleased +## 3.1.0.2 - 2020-04-03 ### Fixed - Fixed an error that could occur when using the `|commerceCurrency` Twig filter if the Intl extension wasn’t enabled. ([#1353])(https://github.com/craftcms/commerce/issues/1353)) diff --git a/composer.json b/composer.json index 8f261e58d0..7e13b8873d 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "craftcms/commerce", "description": "Craft Commerce", "type": "craft-plugin", - "version": "3.1.0.1", + "version": "3.1.0.2", "keywords": [ "cms", "craftcms",