From 97245d70615178891704505e421e6fea5caf3cda Mon Sep 17 00:00:00 2001 From: Alexander Hofbauer Date: Tue, 2 Mar 2021 14:03:59 +0100 Subject: [PATCH] Fix OrderItemsTaxesApplicator with removed units This fixes an undefined offset error caused by holes in ArrayCollection keys after removing units. --- spec/TaxesApplicator/OrderItemsTaxesApplicatorSpec.php | 4 ++-- src/TaxesApplicator/OrderItemsTaxesApplicator.php | 8 +++++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/spec/TaxesApplicator/OrderItemsTaxesApplicatorSpec.php b/spec/TaxesApplicator/OrderItemsTaxesApplicatorSpec.php index 778022bd..117c0237 100644 --- a/spec/TaxesApplicator/OrderItemsTaxesApplicatorSpec.php +++ b/spec/TaxesApplicator/OrderItemsTaxesApplicatorSpec.php @@ -82,7 +82,7 @@ function it_applies_taxes_on_units_based_on_item_total_and_rate( $taxRate->isIncludedInPrice()->willReturn(false); $orderItem->getUnits()->willReturn($units); - $units->getIterator()->willReturn(new \ArrayIterator([$unit1->getWrappedObject(), $unit2->getWrappedObject()])); + $units->getIterator()->willReturn(new \ArrayIterator([4 => $unit1->getWrappedObject(), 5 => $unit2->getWrappedObject()])); $distributor->distribute(100, 2)->willReturn([50, 50]); @@ -191,7 +191,7 @@ function it_does_not_apply_taxes_with_amount_0( $taxRate->isIncludedInPrice()->willReturn(false); $orderItem->getUnits()->willReturn($units); - $units->getIterator()->willReturn(new \ArrayIterator([$unit1->getWrappedObject(), $unit2->getWrappedObject()])); + $units->getIterator()->willReturn(new \ArrayIterator([4 => $unit1->getWrappedObject(), 5 => $unit2->getWrappedObject()])); $distributor->distribute(0, 2)->willReturn([0, 0]); diff --git a/src/TaxesApplicator/OrderItemsTaxesApplicator.php b/src/TaxesApplicator/OrderItemsTaxesApplicator.php index b5f28104..9c07471c 100644 --- a/src/TaxesApplicator/OrderItemsTaxesApplicator.php +++ b/src/TaxesApplicator/OrderItemsTaxesApplicator.php @@ -75,13 +75,15 @@ public function apply(OrderInterface $order, ZoneInterface $zone): void $totalTaxAmount = $this->calculator->calculate($item->getTotal(), $taxRate); $splitTaxes = $this->distributor->distribute($totalTaxAmount, $quantity); + $i = 0; /** @var OrderItemUnitInterface $unit */ - foreach ($item->getUnits() as $index => $unit) { - if (0 === $splitTaxes[$index]) { + foreach ($item->getUnits() as $unit) { + if (0 === $splitTaxes[$i]) { continue; } - $this->addAdjustment($unit, $splitTaxes[$index], $taxRate); + $this->addAdjustment($unit, $splitTaxes[$i], $taxRate); + ++$i; } } }