From 3aec65205c7bf2a91d96ea536afc449f030fc787 Mon Sep 17 00:00:00 2001 From: Tomas Saghy Date: Fri, 1 Jul 2022 23:27:54 +0200 Subject: [PATCH] add rounding --- src/Cart.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/Cart.php b/src/Cart.php index 2a82e1f..f71b96b 100644 --- a/src/Cart.php +++ b/src/Cart.php @@ -61,6 +61,12 @@ class Cart */ protected $_cartModifiedCallback = true; + /** @var callable|null */ + protected $totalRounding; + + /** @var Decimal */ + protected $roundingAmount; + /** * Constructor. * @@ -557,9 +563,25 @@ public function getTotal($type = '~') $total = $total->add($item); } + if (\is_callable($this->totalRounding)) { + $newTotal = \call_user_func($this->totalRounding, $total); + $this->roundingAmount = $newTotal->sub($total); + $total = $newTotal; + } + return $total; } + /** + * @param callable|string $type + */ + public function getRoundingAmount($type = '~'): Decimal + { + $this->getTotal($type); + + return $this->roundingAmount; + } + /** * Get taxes. *