diff --git a/Changelog.md b/Changelog.md index b61e84cb6..f18257752 100644 --- a/Changelog.md +++ b/Changelog.md @@ -47,6 +47,7 @@ - BC: Added the void return type to `Checkout::setShippingAddress()` - BC: Added the `removeShippingAddress()` method to the Checkout interface - BC: The unused `$config` parameter has been removed from the `RequestStore` checkout driver constructor +- BC: Added the `deleteByType()` and `clear()` methods to the `AdjustmentCollection` interface - Fixed possible null return type on Billpayer::getName() when is_organization is true but the company name is null ## 3.x Series diff --git a/src/Adjustments/Changelog.md b/src/Adjustments/Changelog.md index cae1d9ab3..bce4deb15 100644 --- a/src/Adjustments/Changelog.md +++ b/src/Adjustments/Changelog.md @@ -10,6 +10,7 @@ - Dropped Enum v3 Support - Changed minimal Enum requirement to v4.1 - Added the `mapInto()` method to the `RelationAdjustmentCollection` class, which forwards the call to the underlying Eloquent collection +- BC: Added the `deleteByType()` method to the `AdjustmentCollection` interface + both implementation ## 3.x Series diff --git a/src/Adjustments/Contracts/AdjustmentCollection.php b/src/Adjustments/Contracts/AdjustmentCollection.php index 18fc94cd3..3f73059cd 100644 --- a/src/Adjustments/Contracts/AdjustmentCollection.php +++ b/src/Adjustments/Contracts/AdjustmentCollection.php @@ -34,11 +34,9 @@ public function add(Adjustment $adjustment): void; public function remove(Adjustment $adjustment): void; - /** - * @todo Add this method in v4 - * /** Deletes all adjustment items in the collection / - * public function clear(): void - */ + public function clear(): void; + + public function deleteByType(AdjustmentType $type): void; public function first(): ?Adjustment; diff --git a/src/Adjustments/Support/ArrayAdjustmentCollection.php b/src/Adjustments/Support/ArrayAdjustmentCollection.php index 9e4126ca7..38e131933 100644 --- a/src/Adjustments/Support/ArrayAdjustmentCollection.php +++ b/src/Adjustments/Support/ArrayAdjustmentCollection.php @@ -88,6 +88,15 @@ public function clear(): void } } + public function deleteByType(AdjustmentType $type): void + { + foreach ($this->items as $key => $item) { + if ($type->equals($item->getType())) { + unset($this->items[$key]); + } + } + } + public function byType(AdjustmentType $type): AdjustmentCollectionContract { $result = new self($this->adjustable); diff --git a/src/Adjustments/Support/RelationAdjustmentCollection.php b/src/Adjustments/Support/RelationAdjustmentCollection.php index 6b05a6651..0d6119146 100644 --- a/src/Adjustments/Support/RelationAdjustmentCollection.php +++ b/src/Adjustments/Support/RelationAdjustmentCollection.php @@ -90,6 +90,15 @@ public function clear(): void $this->eloquentCollection()->each(fn (Adjustment|Model $adjustment) => $adjustment->delete()); } + public function deleteByType(AdjustmentType $type): void + { + $this->eloquentCollection()->each(function (Adjustment|Model $adjustment) use ($type) { + if ($type->equals($adjustment->getType())) { + $adjustment->delete(); + } + }); + } + public function byType(AdjustmentType $type): AdjustmentCollection { $result = new self($this->model); diff --git a/src/Foundation/Listeners/CalculateShippingFees.php b/src/Foundation/Listeners/CalculateShippingFees.php index ee7274102..6148bfe82 100644 --- a/src/Foundation/Listeners/CalculateShippingFees.php +++ b/src/Foundation/Listeners/CalculateShippingFees.php @@ -40,10 +40,7 @@ public function handle(CheckoutEvent|CartEvent $event): void return; } - $shippingAdjustments = $cart->adjustments()->byType(AdjustmentTypeProxy::SHIPPING()); - foreach ($shippingAdjustments as $adjustment) { - $shippingAdjustments->remove($adjustment); - } + $cart->adjustments()->deleteByType(AdjustmentTypeProxy::SHIPPING()); /** @var ShippingMethod $shippingMethod */ if (null === $shippingMethod = ShippingMethodProxy::find($checkout->getShippingMethodId())) {