0

I've got a basic jQuery script that I use to update the cart, either through the actions /actions/commerce/cart/updateCart (if product isn't in cart yet) or /actions/commerce/cart/updateLineItem (if product is already in cart.

It's been working fine, however I'm noticing the ajax return object is returning with property .success as true even if the product that was added was out of stock. This leaves the user thinking the product was added, rather than being able to say it wasn't and was out of stock.

For example (not the direct code, might have some errors, but the gist of what I am doing is:)

HTML

<form method="POST" id="addToCartForm">
{{ getCsrfInput() }}
<input type="hidden" name="action" value="commerce/cart/updateCart">
<input type="hidden" name="redirect" value="/cart">
<input type="hidden" name="purchasableId" value="{{ product.id }}">
<input type="hidden" name="qty" value="1">
</form>

jQuery .. on submit form

$form = ("#addToCartForm");

var form_data = $form.serializeArray()

form_data.push({
    name: window.csrfTokenName,
    value: window.csrfTokenValue
})


$.post('/actions/commerce/cart/updateCart', form_data, function(response) {

    if(response.success) { 
    console.log("success"); 
    }

    else if(response.error) { 
    console.log(response.error); 
    }

    else {
    console.log("neither a success nor an error returned.. ¯\_(ツ)_/¯");
    }

});

The problem is that if the product has no stock left, response still returns with success. Right now, I figured the best thing I can do is look inside response.cart.lineItems and see if the new purchasableId exists in there. If not, than I can assume it wasn't added and will just have to use that as an indicator that stock was empty - although that'd be an assumption not always true.

Is this a bug or would there be a reason/mistake that could cause commerce to return .success as true even when the product being added is out of stock?

1

This sounds like a bug and I would raise it with support@craftcommerce.com

The stock part of Craft Commerce is still pretty basic at this point.

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.