Skip to content

Commit

Permalink
Fix floating point summing
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeholder committed Dec 11, 2024
1 parent cdb0426 commit af17df6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- Fixed a PHP error that could occur when calculating order totals. ([#3802](https://github.com/craftcms/commerce/issues/3802))
- Fixed a bug where a product’s default price was showing incorrectly on the Products index page. ([#3807](https://github.com/craftcms/commerce/issues/3807))
- Fixed a bug where inline-editable Matrix fields weren’t saving content on product variants. ([#3805](https://github.com/craftcms/commerce/issues/3805))
- Fixed a bug where order errors weren't showing on the Edit Order page.
Expand Down
14 changes: 8 additions & 6 deletions src/elements/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -1700,7 +1700,8 @@ public function updateOrderPaidInformation(): void
$justPaid = $paidInFull && $this->datePaid == null;
$justAuthorized = $authorizedInFull && $this->dateAuthorized == null;

$canComplete = ($this->getTotalAuthorized() + $this->getTotalPaid()) > 0;
$completeTotal = $this->_getTeller()->add($this->getTotalAuthorized(), $this->getTotalPaid());
$canComplete = $this->_getTeller()->greaterThan($completeTotal, 0);

// If it is no longer paid in full, set datePaid to null
if (!$paidInFull) {
Expand Down Expand Up @@ -2662,26 +2663,27 @@ public function getPaidStatusHtml(): string
*/
public function getTotal(): float
{
return Currency::round($this->getItemSubtotal() + $this->getAdjustmentsTotal());
return (float)$this->_getTeller()->add($this->getItemSubtotal(), $this->getAdjustmentsTotal());
}

/**
* Get the total price of the order, whose minimum value is enforced by the configured {@link Store::getMinimumTotalPriceStrategy() strategy set for minimum total price}.
*/
public function getTotalPrice(): float
{
$total = $this->getItemSubtotal() + $this->getAdjustmentsTotal(); // Don't get the pre-rounded total.
$total = (float)$this->_getTeller()->add($this->getItemSubtotal(), $this->getAdjustmentsTotal());
// Don't get the pre-rounded total.
$strategy = $this->getStore()->getMinimumTotalPriceStrategy();

if ($strategy === Store::MINIMUM_TOTAL_PRICE_STRATEGY_ZERO) {
return Currency::round(max(0, $total));
return (float)$this->_getTeller()->max(0, $total);
}

if ($strategy === Store::MINIMUM_TOTAL_PRICE_STRATEGY_SHIPPING) {
return Currency::round(max($this->getTotalShippingCost(), $total));
return (float)$this->_getTeller()->max($this->getTotalShippingCost(), $total);
}

return Currency::round($total);
return $total;
}

public function getItemTotal(): float
Expand Down
3 changes: 2 additions & 1 deletion src/models/LineItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,8 @@ public function getSubtotal(): float
*/
public function getTotal(): float
{
return $this->getSubtotal() + $this->getAdjustmentsTotal();
$teller = Plugin::getInstance()->getCurrencies()->getTeller($this->order->currency);
return (float)$teller->add($this->getSubtotal(), $this->getAdjustmentsTotal());
}

/**
Expand Down

0 comments on commit af17df6

Please sign in to comment.