2

In the Magento 2 cart when you click on the basket you can only remove one product from the cart. What I would like to do is to be able to remove multiple items from the cart at the same time as soon as I click on a single basket.

As shown in the example below, if I have two or more items in my cart, if I click on a single basket I would like more items to be removed. enter image description here

I tried to do it with jquery like this but without success:

<script>
$("#shopping-cart-table .deleteItem .action-delete").on('click',function(){
                        $("#shopping-cart-table .deleteItem .action-delete").each(function(i) {
                            var btn = $(this);
                            
                            setTimeout(function() {
                                btn.trigger("click");
                            }, 100);
                            
                        });
                    });
</script>

How could I do fro remove multiple items from cart when i click only on remove button?

2
  • Are you saying you want to empty your cart? or you want to delete 2 out 3 items for instance..? but in the latter case, what is your business rule? Sep 8, 2022 at 13:03
  • I want to delete two out of three articles for example, that's right. My rule is: "When I click the remove button a simple product and a virtual product that have the same attribute value must be removed from cart."
    – Jackom
    Sep 8, 2022 at 13:05

2 Answers 2

1

ok, it is quite complex but the structure should be:

  1. you need to add a custom controller
  2. you need to call this custom route and the controller will call the quote session model and browse its items and remove them if relevant. When the whole operation is complete, you may call collectTotals on the quote
  3. when the controller response is successful, you will need to refresh the cart and this requires knowledge of uiComponent/customer-data and both are not trivial

One by one, all the points above do have a lot of questions/answers that you will find no doubts; you should be able to work your way through. Event the last point has some answers.. good luck

7
  • Thanks for the reply. I have tried this method several times and it does not always work well because it creates problems for those with slow connections. I noticed that when you quickly click on all the baskets of items in the Magento cart I remove all items correctly with just one page refresh. Is there any way to invoke the original Magento $block->getDeletePostJson() method for multiple articles that have the same attribute value?
    – Jackom
    Sep 8, 2022 at 13:18
  • enable the cache is your answer :-) Sep 8, 2022 at 13:19
  • yes I have always enabled the cache but some customers who have slow PCs had problems in these types of operations. There is an alternative? Thanks
    – Jackom
    Sep 8, 2022 at 13:21
  • Get a small linode server for $30/month and it should be fast enough Sep 8, 2022 at 14:31
  • I am on a super fast Amazon server. The problem persists for some customers with slow connections
    – Jackom
    Sep 8, 2022 at 15:00
0

magento default supports this feature, it is called Clear Shopping Cart, but it is hidden by default.

For magento version >= 2.4.1, you can config it in Admin menu > Stores > Configuration > Sales > Checkout > Shopping Cart group > Enable Clear Shopping Cart field.

For magento version < 2.4.1, you have to display it by CSS:

.checkout-cart-index .form-cart .cart .action.clear {
    display: block
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.