Skip to content

Commit

Permalink
include locale value normalization
Browse files Browse the repository at this point in the history
Fixes $3789
  • Loading branch information
lukeholder committed Dec 4, 2024
1 parent 9943fc0 commit 4c12360
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes for Craft Commerce

## Unreleased

- Fixed a bug where the price was not formatted correctly according to the locale in the payment model on the Order Edit screens. ([#3789](https://github.com/craftcms/commerce/issues/3789))

## 5.2.7 - 2024-11

- Fixed an error that occurred on the Orders index page when running Craft CMS 5.5.4 or later. ([#3793](https://github.com/craftcms/commerce/issues/3793))
Expand Down
4 changes: 4 additions & 0 deletions src/controllers/OrdersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -1236,11 +1236,15 @@ public function actionPaymentAmountData(): Response
$paymentCurrencies = Plugin::getInstance()->getPaymentCurrencies();
$paymentCurrency = $this->request->getRequiredParam('paymentCurrency');
$paymentAmount = $this->request->getRequiredParam('paymentAmount');
$locale = $this->request->getRequiredParam('locale');
$orderId = $this->request->getRequiredParam('orderId');
/** @var Order $order */
$order = Order::find()->id($orderId)->one();
$baseCurrency = $order->currency;

$paymentAmount = MoneyHelper::toMoney(['value' => $paymentAmount, 'currency' => $baseCurrency, 'locale' => $locale]);
$paymentAmount = MoneyHelper::toDecimal($paymentAmount);

$baseCurrencyPaymentAmount = $paymentCurrencies->convertCurrency($paymentAmount, $paymentCurrency, $baseCurrency);

Check failure on line 1248 in src/controllers/OrdersController.php

View workflow job for this annotation

GitHub Actions / ci / Code Quality / PHPStan / PHPStan

Parameter #1 $amount of method craft\commerce\services\PaymentCurrencies::convertCurrency() expects float, string|false given.
$baseCurrencyPaymentAmountAsCurrency = Craft::t('commerce', 'Pay {amount} of {currency} on the order.', ['amount' => Currency::formatAsCurrency($baseCurrencyPaymentAmount, $baseCurrency), 'currency' => $baseCurrency]);

Expand Down
14 changes: 11 additions & 3 deletions src/templates/_components/gateways/_modalWrapper.twig
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@
{{ formHtml|raw }}

<fieldset>
<legend>Payment Amount</legend>
<legend>{{ "Payment Amount"|t('commerce') }}</legend>
<div>
<input type="text" class="paymentAmount text" name="paymentAmount" autocomplete="off" placeholder="{{ order.outstandingBalance }}" step="any" min="1" max="{{ order.outstandingBalance }}" value="{{ order.getPaymentAmount() }}" style="margin: 0 0 0 5px; width:{{ 65 + (10*order.outstandingBalance|length) }}px;">
<input type="hidden" name="paymentAmount[locale]" value="{{ craft.app.formattingLocale.id }}" />
<input type="text" class="paymentAmount text"
name="paymentAmount[value]"
autocomplete="off"
placeholder="{{ order.outstandingBalance }}"
step="any" min="1" max="{{ order.outstandingBalance }}"
value="{{ order.getPaymentAmount() }}" style="margin: 0 0 0 5px; width:{{ 65 + (10*order.outstandingBalance|length) }}px;">

{% set currencies = craft.commerce.paymentCurrencies.getAllPaymentCurrencies() %}
{% set primaryCurrency = craft.commerce.paymentCurrencies.getPrimaryPaymentCurrency() %}
Expand Down Expand Up @@ -77,7 +83,8 @@


function updatePrice(form) {
var price = form.find("input.paymentAmount").val();
var price = form.find("input[name='paymentAmount[value]']").val();
var locale = form.find("input[name='paymentAmount[locale]']").val();

$.ajax({
type: "POST",
Expand All @@ -89,6 +96,7 @@
data: {
'action' : 'commerce/orders/payment-amount-data',
'paymentAmount': price,
'locale': locale,
'paymentCurrency': form.find(".paymentCurrency").val(),
'orderId' : orderId
},
Expand Down

0 comments on commit 4c12360

Please sign in to comment.