1

Ok, I'm getting pretty crazy here. I'm trying to remove a variant from the cart. Adding works fine, like this:

<input type="hidden" name="purchasableId" value="{{ variant.id }}" id="{{ variant.sku }}">
<input type="submit">

(variant is an item from product.variants)

However, when I want to remove an item, I do it like this:

<input type="hidden" name="lineItems[{{ variant.id }}][qty]" value="0">
<input type="submit">

But nothing gets removed. The weird thing is, when I query for lineItems, there's an item with the id "92". The id from the variant is 19874. So that's the id I use in the hidden qty field. I have absolutely no idea where this 92 comes from. It is not the variant id. It's alo snot the product id. I don't know what it is. What am I doing wrong here?

2

You can remove a line item by including a remove parameter in the request. This example adds a checkbox the customer can use to remove the line item from the cart:

{% set cart = craft.commerce.carts.cart %}
<form method="post">
    {{ csrfInput() }}
    {{ actionInput('commerce/cart/update-cart') }}
    {% for item in cart.lineItems %}
        <input type="number" name="lineItems[{{ item.id }}][qty]" min="1" value="{{ item.qty }}">
        <input type="text" name="lineItems[{{ item.id }}][note]" placeholder="My Note" value="{{ item.note }}">
        <input type="checkbox" name="lineItems[{{ item.id }}][remove]" value="1"> Remove item<br>
    {% endfor %}
    <button type="submit">Update Line Item</button>
</form>

You can check more on this on craft commerce documentation by using below link. https://craftcms.com/docs/commerce/3.x/orders-carts.html#carts

1
  • Thank you. Yes I found that in the docs. But that still means I need the lineItem ID for the item I want to remove. And I don't know that id, since I am on a product detail page and want to remove only that product. On the product page I only know the variant properties. I could loop through all the lineItems and filter out the lineItem where the purchasableId = the variant id. But that doesn't sound like it is the best option there is. There must be something easier, no? Dec 22 '20 at 8:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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