Skip to content

Commit

Permalink
Merge pull request #3782 from craftcms/feature/pt-2310-5x-wrong-decim…
Browse files Browse the repository at this point in the history
…al-position-for-product-variant-between-read

Fixed #3768 inline editing variant prices with localized formatting
  • Loading branch information
nfourtythree authored Nov 21, 2024
2 parents 2cf8bb8 + badeded commit e598c90
Show file tree
Hide file tree
Showing 2 changed files with 22 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 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.
Expand Down
21 changes: 18 additions & 3 deletions src/base/Purchasable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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 : '',
Expand Down

0 comments on commit e598c90

Please sign in to comment.