From badedede48c270f9094a1489743afb39f59f4bc6 Mon Sep 17 00:00:00 2001 From: Nathaniel Hammond Date: Thu, 21 Nov 2024 15:14:30 +0000 Subject: [PATCH] Fixed #3768 inline editing variant prices with localized formatting --- CHANGELOG.md | 4 ++++ src/base/Purchasable.php | 21 ++++++++++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 913d9dcb86..8030019c56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Release Notes for Craft Commerce +## Unreleased + +- Fixed a bug where prices could display incorrectly when inline editing a variant. ([#3768](https://github.com/craftcms/commerce/issues/3768)) + ## 5.2.5 - 2024-11-20 - The `resave/products`, `resave/orders`, and `resave/carts` commands now support the `--with-fields` option. diff --git a/src/base/Purchasable.php b/src/base/Purchasable.php index dcae3e164f..ce7906982c 100644 --- a/src/base/Purchasable.php +++ b/src/base/Purchasable.php @@ -329,15 +329,30 @@ public function setAttributesFromRequest(array $values): void */ protected function inlineAttributeInputHtml(string $attribute): string { + $localizePrice = function(string $attribute) { + $price = $this->{$attribute}; + if (empty($this->getErrors($attribute))) { + if ($price === null && $attribute === 'basePromotionalPrice') { + return null; + } elseif ($price === null) { + $price = 0; + } + + $price = Craft::$app->getFormatter()->asDecimal($price); + } + + return $price; + }; + return match ($attribute) { 'availableForPurchase' => PurchasableHelper::availableForPurchaseInputHtml($this->availableForPurchase), - 'price' => Currency::moneyInputHtml($this->basePrice, [ + 'price' => Currency::moneyInputHtml($localizePrice('basePrice'), [ 'id' => 'base-price', 'name' => 'basePrice', 'currency' => $this->getStore()->getCurrency()->getCode(), 'currencyLabel' => $this->getStore()->getCurrency()->getCode(), ]), - 'promotionalPrice' => Currency::moneyInputHtml($this->basePromotionalPrice, [ + 'promotionalPrice' => Currency::moneyInputHtml($localizePrice('basePromotionalPrice'), [ 'id' => 'base-promotional-price', 'name' => 'basePromotionalPrice', 'currency' => $this->getStore()->getCurrency()->getCode(), @@ -1262,7 +1277,7 @@ protected function attributeHtml(string $attribute): string return match ($attribute) { 'sku' => (string)Html::encode($this->getSkuAsText()), 'price' => $this->basePriceAsCurrency, - 'promotionalPrice' => $this->basePromotionalPriceAsCurrency, + 'promotionalPrice' => $this->basePromotionalPrice !== null ? $this->basePromotionalPriceAsCurrency : '', 'weight' => $this->weight !== null ? Craft::$app->getFormattingLocale()->getFormatter()->asDecimal($this->$attribute) . ' ' . Plugin::getInstance()->getSettings()->weightUnits : '', 'length' => $this->length !== null ? Craft::$app->getFormattingLocale()->getFormatter()->asDecimal($this->$attribute) . ' ' . Plugin::getInstance()->getSettings()->dimensionUnits : '', 'width' => $this->width !== null ? Craft::$app->getFormattingLocale()->getFormatter()->asDecimal($this->$attribute) . ' ' . Plugin::getInstance()->getSettings()->dimensionUnits : '',