How do I make a "Clear Cart" button in the shopping card section?

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
4 years ago
How do I make a "Clear Cart" button in the shopping card section?
Not: Version 4.20

4 years ago
You need to add a button on the cart page and make a request when clicked to a method that deletes all the items of the cart for the current user. You can do so by
foreach (var item in _workContext.CurrentCustomer.ShoppingCartItems)
                _shoppingCartService.DeleteShoppingCartItem(item.Id);


and reload the page or you can make use of ajax
4 years ago
sanju.dahal741 wrote:
You need to add a button on the cart page and make a request when clicked to a method that deletes all the items of the cart for the current user. You can do so by
foreach (var item in _workContext.CurrentCustomer.ShoppingCartItems)
                _shoppingCartService.DeleteShoppingCartItem(item.Id);


and reload the page or you can make use of ajax


I'm a rookie. The questions may sound stupid. Sorry. Where do I add this code?
4 years ago
Well, you need to create an action method in a new controller or any existing controller may be in the ShoppingCartController(I am assuming you are modifying the source code rather than using a plugin for this).
 public virtual IActionResult ClearShoppingCart()
        {
         if(!_permissionService.Authorize(StandardPermissionProvider.ManageCurrentCarts))
                return AccessDeniedDataTablesJson();

            foreach (var item in _workContext.CurrentCustomer.ShoppingCartItems)
                _shoppingCartService.DeleteShoppingCartItem(item.Id);

            return RedirectToRoute("ShoppingCart");
        }


and invoke this method when the clear shopping cart button is clicked
4 years ago
sanju.dahal741 wrote:
Well, you need to create an action method in a new controller or any existing controller may be in the ShoppingCartController(I am assuming you are modifying the source code rather than using a plugin for this).
 public virtual IActionResult ClearShoppingCart()
        {
         if(!_permissionService.Authorize(StandardPermissionProvider.ManageCurrentCarts))
                return AccessDeniedDataTablesJson();

            foreach (var item in _workContext.CurrentCustomer.ShoppingCartItems)
                _shoppingCartService.DeleteShoppingCartItem(item.Id);

            return RedirectToRoute("ShoppingCart");
        }


and invoke this method when the clear shopping cart button is clicked


I did as you said. But it didn't work. What did I do wrong?

4 years ago
I assume that the ActionMethod does not hit.  Because you do not add

        [HttpPost, ActionName("Cart")]
        [FormValueRequired("ClearShoppingCart")]

annotation/attributes on top of the IActionMethod. Change code is

  
[HttpPost, ActionName("Cart")]
        [FormValueRequired("ClearShoppingCart")]
        public virtual IActionResult ClearShoppingCart()
        {
           if (!_permissionService.Authorize(StandardPermissionProvider.ManageCurrentCarts))
                return AccessDeniedDataTablesJson();

            var shoppingCartItem = _workContext.CurrentCustomer.ShoppingCartItems.ToList();
            foreach (var item in shoppingCartItem)
                _shoppingCartService.DeleteShoppingCartItem(item.Id);

  
            return RedirectToRoute("ShoppingCart");
        }


Keep the cshtml as same. Because the button name is important. If you change the button name you also need to change the annotation.
4 years ago
sina.islam wrote:
I assume that the ActionMethod does not hit.  Because you do not add

        [HttpPost, ActionName("Cart")]
        [FormValueRequired("ClearShoppingCart")]

annotation/attributes on top of the IActionMethod. Change code is

  
[HttpPost, ActionName("Cart")]
        [FormValueRequired("ClearShoppingCart")]
        public virtual IActionResult ClearShoppingCart()
        {
           if (!_permissionService.Authorize(StandardPermissionProvider.ManageCurrentCarts))
                return AccessDeniedDataTablesJson();

            var shoppingCartItem = _workContext.CurrentCustomer.ShoppingCartItems.ToList();
            foreach (var item in shoppingCartItem)
                _shoppingCartService.DeleteShoppingCartItem(item.Id);

  
            return RedirectToRoute("ShoppingCart");
        }


Keep the cshtml as same. Because the button name is important. If you change the button name you also need to change the annotation.


Thank you. Now it's OK.
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.