Skip to content

Commit ed5ffed

Browse files
committed
6965: #6965: \Magento\Directory\Model\PriceCurrency::format() fails without conversion rate
1 parent 2c4fa07 commit ed5ffed

File tree

1 file changed

+28
-23
lines changed

1 file changed

+28
-23
lines changed

Diff for: app/code/Magento/Directory/Model/PriceCurrency.php

+28-23
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,7 @@ public function format(
7777
$scope = null,
7878
$currency = null
7979
) {
80-
if ($currency instanceof Currency) {
81-
$currentCurrency = $currency;
82-
} elseif (is_string($currency)) {
83-
$currentCurrency = $this->currencyFactory->create()->load($currency);
84-
} else {
85-
$currentCurrency = $this->getStore($scope)->getCurrentCurrency();
86-
}
87-
88-
return $currentCurrency->formatPrecision($amount, $precision, [], $includeContainer);
80+
return $this->createCurrency($scope, $currency)->formatPrecision($amount, $precision, [], $includeContainer);
8981
}
9082

9183
/**
@@ -108,20 +100,7 @@ public function convertAndFormat(
108100
*/
109101
public function getCurrency($scope = null, $currency = null)
110102
{
111-
if ($currency instanceof Currency) {
112-
$currentCurrency = $currency;
113-
} elseif (is_string($currency)) {
114-
$currency = $this->currencyFactory->create()
115-
->load($currency);
116-
$baseCurrency = $this->getStore($scope)
117-
->getBaseCurrency();
118-
$currentCurrency = $baseCurrency->getRate($currency) ? $currency : $baseCurrency;
119-
} else {
120-
$currentCurrency = $this->getStore($scope)
121-
->getCurrentCurrency();
122-
}
123-
124-
return $currentCurrency;
103+
return $this->createCurrency($scope, $currency, true);
125104
}
126105

127106
/**
@@ -164,4 +143,30 @@ public function round($price)
164143
{
165144
return round($price, 2);
166145
}
146+
147+
/**
148+
* Get currency considering currency rate configuration.
149+
*
150+
* @param null|string|bool|int|\Magento\Framework\App\ScopeInterface $scope
151+
* @param \Magento\Framework\Model\AbstractModel|string|null $currency
152+
* @param bool $includeRate
153+
*
154+
* @return Currency
155+
*/
156+
private function createCurrency($scope, $currency, bool $includeRate = false)
157+
{
158+
if ($currency instanceof Currency) {
159+
$currentCurrency = $currency;
160+
} elseif (is_string($currency)) {
161+
$currentCurrency = $this->currencyFactory->create()->load($currency);
162+
if ($includeRate) {
163+
$baseCurrency = $this->getStore($scope)->getBaseCurrency();
164+
$currentCurrency = $baseCurrency->getRate($currentCurrency) ? $currentCurrency : $baseCurrency;
165+
}
166+
} else {
167+
$currentCurrency = $this->getStore($scope)->getCurrentCurrency();
168+
}
169+
170+
return $currentCurrency;
171+
}
167172
}

0 commit comments

Comments
 (0)