How do you reset or empty the cart, in Commerce 2? There is no mention of this in the documentation.
In Commerce 1, it was possible to issue a request to the commerce/cart/removeAllLineItems action to clear the cart, but this no longer works.
How do you reset or empty the cart, in Commerce 2? There is no mention of this in the documentation.
In Commerce 1, it was possible to issue a request to the commerce/cart/removeAllLineItems action to clear the cart, but this no longer works.
Depending on context, here's another handy way of clearing the cart for Commerce 2/3
In a template, you can put:
{% do cart.setLineItems([]) %}
{% do craft.app.elements.saveElement(cart) %}
You could e.g. just use a direct link to a template that does that, and in turn re-directs to wherever...if you don't want to muck around with a form.
The commerce/cart/remove-all-line-items action (or in Craft 2 terms, commerce/cart/removeAllLineItems) was deprecated (and subsequently removed in Commerce 3), so it's up to you to build a form to explicitly delete all line items:
<form method="post">
{{ csrfInput() }}
{{ actionInput('commerce/cart/update-cart') }}
{% for lineItem in craft.commerce.carts.cart.lineItems %}
<input type="hidden" name="lineItems[{{ lineItem.id }}][remove]" value="✓">
{% endfor %}
<button type="submit">Empty Cart</button>
</form>
This snippet just loops over the current line items, and outputs a hidden input element, which sends the proper POST value to the update-cart action.
It's also possible to give your customer control over which line items are to be removed by changing the input to a check-box (and providing adequate labeling).
The commerce/cart/remove-all-line-itemschanged? I get this "Unable to resolve the request: commerce/cart/remove-all-line-items" when I try to empty my cart. This worked previously for me, but not anymore. – Darryl Hardin May 08 '20 at 17:53update-cardaction. – August Miller May 09 '20 at 02:15