Skip to content

Commit

Permalink
Added the deleteByType() and clear() methods to the `AdjustmentCo…
Browse files Browse the repository at this point in the history
…llection` interface
  • Loading branch information
fulopattila122 committed Feb 26, 2024
1 parent 9cd4eab commit 7b9e4f1
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 9 deletions.
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/Adjustments/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 3 additions & 5 deletions src/Adjustments/Contracts/AdjustmentCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
9 changes: 9 additions & 0 deletions src/Adjustments/Support/ArrayAdjustmentCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions src/Adjustments/Support/RelationAdjustmentCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 1 addition & 4 deletions src/Foundation/Listeners/CalculateShippingFees.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())) {
Expand Down

0 comments on commit 7b9e4f1

Please sign in to comment.