1

I may be missing something obvious but I'm trying to update a +/- quantity button system for adding products to work on Craft Commerce 3

The desired functionality is that click '+' increases the cart amount for that item by one, clicking '-' decreases the cart amount for that item by one.

The original build was in commerce 1, and the apparent way it was written was just by running an update cart ajax call (at what was then 'commerce/cart/updateCart') and passing through a '1' or '-1' as the quantity value. That doesn't appear to work anymore. The '+' call will add the '1', but passing the '-1' through for the '-' call does nothing. It just returns the same cart as if I passed through a zero.

I tried with 'commerce/cart/remove-line-item' but that removes all quantities of the line item rather than just decrement it by one.

I've tried passing the following into an ajax post request:

var data = {
            ['CRAFT_CSRF_TOKEN']: token,
            action: 'commerce/cart/update-cart',
            lineItems: {[lnid]: {'qty': dec}}
        };

With lnid being the line item id and dec being the new desired quantity (I also tried -1 there) but that just destroys the line item completely.

3

Try just POSTing the new absolute quantity you want to change to - e.g. if you have 5 in your cart and want to decrement by one, you'd POST something along these lines:

{
    "redirect": "6bb2a73aac3479c68e0946e4455624e2c1b840bc2b6356074a51ba01ee1234d/cart",
    "action": "commerce/cart/update-cart",
    "lineItems[82503][qty]": "4",
    "lineItems[82503][options][note]": ""
}

That's what we do, and it works.

Result:

{
    "lineItems": [
        {
            "id": 82503,
            "weight": 0,
            "length": 0,
            "height": 0,
            "width": 0,
            "qty": 4,
            "note": "",
            "privateNote": "",
            "purchasableId": "5115",
            "orderId": 145817,
            "lineItemStatusId": null,
            "taxCategoryId": 1,
            "shippingCategoryId": 1,
            "dateCreated": "2020-10-29T11:55:02+11:00",
            "dateUpdated": "2020-11-25T11:34:47+11:00",
            ...etc
}

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.