Skip to content

Commit

Permalink
simplcommerce#713 lock cart on checkout: apply when update quantity, …
Browse files Browse the repository at this point in the history
…revmove, apply coupon
  • Loading branch information
thiennn committed Feb 24, 2019
1 parent 462bfe3 commit 6dcfdb9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ public async Task<IActionResult> List()
public async Task<IActionResult> UpdateQuantity([FromBody] CartQuantityUpdate model)
{
var currentUser = await _workContext.GetCurrentUser();
var cart = await _cartService.GetActiveCart(currentUser.Id);

if (cart == null)
{
return NotFound();
}

if (cart.LockedOnCheckout)
{
return CreateCartLockedResult();
}

var cartItem = _cartItemRepository.Query().FirstOrDefault(x => x.Id == model.CartItemId && x.Cart.CreatedById == currentUser.Id);
if (cartItem == null)
{
Expand All @@ -99,7 +111,7 @@ public async Task<IActionResult> UpdateQuantity([FromBody] CartQuantityUpdate mo
}

[HttpPost("cart/apply-coupon")]
public async Task<ActionResult> ApplyCoupon([FromBody] ApplyCouponForm model)
public async Task<IActionResult> ApplyCoupon([FromBody] ApplyCouponForm model)
{
var currentUser = await _workContext.GetCurrentUser();
var cart = await _cartService.GetActiveCart(currentUser.Id);
Expand All @@ -108,6 +120,11 @@ public async Task<ActionResult> ApplyCoupon([FromBody] ApplyCouponForm model)
return NotFound();
}

if (cart.LockedOnCheckout)
{
return CreateCartLockedResult();
}

var validationResult = await _cartService.ApplyCoupon(cart.Id, model.CouponCode);
if (validationResult.Succeeded)
{
Expand Down Expand Up @@ -137,6 +154,17 @@ public async Task<IActionResult> SaveOrderNote([FromBody] SaveOrderNote model)
public async Task<IActionResult> Remove([FromBody] long itemId)
{
var currentUser = await _workContext.GetCurrentUser();
var cart = await _cartService.GetActiveCart(currentUser.Id);
if (cart == null)
{
return NotFound();
}

if (cart.LockedOnCheckout)
{
return CreateCartLockedResult();
}

var cartItem = _cartItemRepository.Query().FirstOrDefault(x => x.Id == itemId && x.Cart.CreatedById == currentUser.Id);
if (cartItem == null)
{
Expand All @@ -148,5 +176,10 @@ public async Task<IActionResult> Remove([FromBody] long itemId)

return await List();
}

private IActionResult CreateCartLockedResult()
{
return Ok(new { Error = true, Message = "Cart is locked for checkout. Please complete or cancel the checkout first" });
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@
vm.cart = {};

function cartDataCallback(result) {
vm.cart = result.data;
$('.cart-badge .badge').text(vm.cart.items.length);
if (result.data.error) {
toastr.error(result.data.message);
}
else {
vm.cart = result.data;
$('.cart-badge .badge').text(vm.cart.items.length);
}
}

function getShoppingCartItems() {
Expand All @@ -22,16 +27,14 @@
};

vm.increaseQuantity = function increaseQuantity(item) {
item.quantity += 1;
shoppingCartService.updateQuantity(item.id, item.quantity).then(cartDataCallback);
shoppingCartService.updateQuantity(item.id, item.quantity + 1 ).then(cartDataCallback);
};

vm.decreaseQuantity = function decreaseQuantity(item) {
if (item.quantity <= 1) {
return;
}
item.quantity -= 1;
shoppingCartService.updateQuantity(item.id, item.quantity).then(cartDataCallback);
shoppingCartService.updateQuantity(item.id, item.quantity - 1).then(cartDataCallback);
};

vm.applyCoupon = function applyCoupon() {
Expand Down

0 comments on commit 6dcfdb9

Please sign in to comment.