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

<p>The stock part of Craft Commerce is still pretty basic at this point.</p>

CC BY-SA 3.0
2016-12-13T07:38:26.860Z
2016-12-13T07:38:26.860Z
Jeremy Daalder
<p>I've got a basic jQuery script that I use to update the cart, either through the actions <code>/actions/commerce/cart/updateCart</code> (if product isn't in cart yet) or <code>/actions/commerce/cart/updateLineItem</code> (if product is already in cart.</p>

<p>It's been working fine, however I'm noticing the ajax return object is returning with property <code>.success</code> 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.</p>

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

<p>HTML</p>

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

<p>jQuery .. on submit form</p>

<pre><code>$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.. ¯\_(ツ)_/¯");
    }

});
</code></pre>

<p>The problem is that if the product has no stock left, <code>response</code> still returns with <code>success</code>. Right now, I figured the best thing I can do is look inside <code>response.cart.lineItems</code> 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.</p>

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

<plugin-craftcommerce><ajax><jquery>
Commerce - Ajax returning obj.success even when failed
# Commerce - Ajax returning obj.success even when failed

(Asked by: username_0 on 2016-12-11)

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

{{ getCsrfInput() }}






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?

**Tags:** plugin-craftcommerce, ajax, jquery

---

## 1 answers

### Answer by username_1 on 2016-12-13. Score: 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.


---

