Skip to content

Commit 1024667

Browse files
ledai091claude
andcommitted
fix(cart): clear cart after successful payment and preserve coupon on checkout
- Add ClearCart API endpoint to remove all cart items by userId - Call ClearCartAsync in Confirmation page after payment approved - Add CouponCode hidden field in CheckOut.cshtml for clarity Fixes: 1. Cart items not being removed after successful payment 2. Coupon code display consistency in checkout flow 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 82f9a14 commit 1024667

5 files changed

Lines changed: 50 additions & 2 deletions

File tree

Cherry.Services.ShoppingCartAPI/Controllers/CartAPIController.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,10 +190,41 @@ public async Task<ResponseDto> RemoveCart([FromBody]int cartDetailsId)
190190
if(totalCountofCartItem == 1)
191191
{
192192
var cartHeaderToRemove=await _db.CartHeaders.FirstOrDefaultAsync(
193-
u=>u.CartHeaderId==cartDetails.CartHeaderId);
193+
u=>u.CartHeaderId==cartDetails.CartHeaderId);
194194
_db.CartHeaders.Remove(cartHeaderToRemove);
195195
}
196-
await _db.SaveChangesAsync();
196+
await _db.SaveChangesAsync();
197+
_response.Result = true;
198+
}
199+
catch (Exception ex)
200+
{
201+
_response.Message = ex.Message.ToString();
202+
_response.IsSuccess = false;
203+
}
204+
return _response;
205+
}
206+
207+
[HttpDelete("ClearCart/{userId}")]
208+
public async Task<ResponseDto> ClearCart(string userId)
209+
{
210+
try
211+
{
212+
var cartHeader = await _db.CartHeaders
213+
.FirstOrDefaultAsync(u => u.UserId == userId);
214+
215+
if (cartHeader != null)
216+
{
217+
// Remove all cart details associated with this cart header
218+
var cartDetails = _db.CartDetails
219+
.Where(u => u.CartHeaderId == cartHeader.CartHeaderId);
220+
_db.CartDetails.RemoveRange(cartDetails);
221+
222+
// Remove the cart header
223+
_db.CartHeaders.Remove(cartHeader);
224+
225+
await _db.SaveChangesAsync();
226+
}
227+
197228
_response.Result = true;
198229
}
199230
catch (Exception ex)

Cherry.Web/Controllers/CartController.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ public async Task<IActionResult> Confirmation(int orderId)
9797
OrderHeaderDto orderHeader = JsonConvert.DeserializeObject<OrderHeaderDto>(Convert.ToString(response.Result));
9898
if(orderHeader.Status == SD.Status_Approved)
9999
{
100+
// Clear the cart after successful payment
101+
var userId = User.Claims.Where(u => u.Type == JwtRegisteredClaimNames.Sub)?.FirstOrDefault()?.Value;
102+
if (!string.IsNullOrEmpty(userId))
103+
{
104+
await _cartService.ClearCartAsync(userId);
105+
}
100106
return View(orderId);
101107
}
102108
}

Cherry.Web/Service/CartService.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,14 @@ public CartService(IBaseService baseService)
5959
Url = SD.ShoppingCartAPIBase + "/api/cart/CartUpsert"
6060
});
6161
}
62+
63+
public async Task<ResponseDto?> ClearCartAsync(string userId)
64+
{
65+
return await _baseService.SendAsync(new RequestDto()
66+
{
67+
ApiType = SD.ApiType.DELETE,
68+
Url = SD.ShoppingCartAPIBase + "/api/cart/ClearCart/" + userId
69+
});
70+
}
6271
}
6372
}

Cherry.Web/Service/IService/ICartService.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@ public interface ICartService
99
Task<ResponseDto?> RemoveFromCartAsync(int cartDetailsId);
1010
Task<ResponseDto?> ApplyCouponAsync(CartDto cartDto);
1111
Task<ResponseDto?> EmailCart(CartDto cartDto);
12+
Task<ResponseDto?> ClearCartAsync(string userId);
1213
}
1314
}

Cherry.Web/Views/Cart/CheckOut.cshtml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
{
66
<input asp-for="CartHeader.UserId" hidden />
77
<input asp-for="CartHeader.CartHeaderId" hidden />
8+
<input asp-for="CartHeader.CouponCode" hidden />
89
<input asp-for="CartHeader.Discount" hidden />
910
<input asp-for="CartHeader.CartTotal" hidden />
1011
}

0 commit comments

Comments
 (0)