Skip to content

Commit 789db4e

Browse files
committed
fix: remove only selected items from cart after checkout
1 parent e909d93 commit 789db4e

4 files changed

Lines changed: 28 additions & 2 deletions

File tree

app/Repositories/CartRepository.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,18 @@ public function getSelectedUserCart(int $userId): Collection
7474
->with('product')
7575
->get();
7676
}
77+
78+
/**
79+
* Remove selected items from user's cart
80+
*
81+
* @param int $userId
82+
* @return bool
83+
*/
84+
public function removeSelectedCartItems(int $userId): bool
85+
{
86+
return $this->query()
87+
->where('user_id', $userId)
88+
->where('selected', true)
89+
->delete() > 0;
90+
}
7791
}

app/Repositories/Interfaces/CartRepositoryInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ public function getUserCart(int $userId): Collection;
1111
public function findByUserAndItem(int $userId, int $itemId): ?Cart;
1212
public function clearUserCart(int $userId): bool;
1313
public function getSelectedUserCart(int $userId): Collection;
14+
public function removeSelectedCartItems(int $userId): bool;
1415
}

app/Services/Customer/CartService.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,15 @@ public function getSelectedCartTotal(int $userId): float
201201
return $cart->quantity * $cart->price;
202202
});
203203
}
204+
205+
/**
206+
* Remove selected items from cart
207+
*
208+
* @param int $userId
209+
* @return bool
210+
*/
211+
public function removeSelectedItems(int $userId): bool
212+
{
213+
return $this->cartRepo->removeSelectedCartItems($userId);
214+
}
204215
}

app/Services/Customer/CheckoutService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ public function processCheckout(int $userId, array $checkoutData): int
7676
]);
7777
}
7878

79-
// Clear cart after successful order
80-
$this->cartRepo->clearUserCart($userId);
79+
// Clear selected items from cart after successful order
80+
$this->cartRepo->removeSelectedCartItems($userId);
8181

8282
DB::commit();
8383

0 commit comments

Comments
 (0)