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.

up vote 2 down vote accepted

The commerce/cart/remove-all-line-items action (or in Craft 2 terms, commerce/cart/removeAllLineItems) is deprecated, so it's up to you to build a form to explicitly delete all line items:

<form method="post">
    {{ csrfInput() }}
    <input type="hidden" name="action" value="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).

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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