What I am trying to achieve is the following functionality, where by if a user clicks the red remove button next to an item, the cart is updated, and the item is removed.
I have a fresh install of NopCom 2.10, and ready to start experimenting. I have made the following change to the [ UpdateCart ] Action inside the [ ShoppingCartControllers ].
[ValidateInput(false)]
[HttpPost, ActionName("Cart")]
[FormValueRequired("updatecart")]
public ActionResult UpdateCart(FormCollection form, int prdId)
{
var cart = _workContext.CurrentCustomer.ShoppingCartItems.Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart).ToList();
foreach (var sci in cart)
{
bool remove = (prdId == sci.Id) ? true : false;
if (remove)
_shoppingCartService.DeleteShoppingCartItem(sci, true);
else
{
int newQuantity = sci.Quantity;
foreach (string formKey in form.AllKeys)
if (formKey.Equals(string.Format("itemquantity{0}", sci.Id), StringComparison.InvariantCultureIgnoreCase))
{
int.TryParse(form[formKey], out newQuantity);
break;
}
_shoppingCartService.UpdateShoppingCartItem(_workContext.CurrentCustomer,
sci.Id, newQuantity, true);
}
}
//updated cart
cart = _workContext.CurrentCustomer.ShoppingCartItems.Where(sci => sci.ShoppingCartType == ShoppingCartType.ShoppingCart).ToList();
var model = PrepareShoppingCartModel(new ShoppingCartModel(), cart, true);
return View(model);
}
As you can see, the Action now accepts two parameters, one is the Cart Form, and the other is the ID of the product to be removed. So, now I have two questions:
1) Is my approach correct ? or are there better, simpler ways ?
2) If my approach is correct, how would I go further in this ? cause right now I don't know how to submit the form, whilst also passing in the ID of the product item to be removed.
As always, any help would be greatly appreciated.
Thank You.