Out of Stock error not triggering when adding to cart with line items

Highlighted
New Member
7 0 0

Lets say I have a product with an inventory of 2, and not allow customers to purchase when its out of stock.

If I attempt to add 3 of them using the AJAX Api WITHOUT any line items. I get the error saying I can only add up to 2 of them.

If I attempt to add 3 of them using the AJAX Api WITH line items. I can successfully add 3 to to my cart. It isn't until checkout where it tells me that I exceeded the existing quantity.

Heres another thread with the same issue from 2 years ago: https://ecommerce.shopify.com/c/shopify-apis-and-technology/t/possible-bug-with-out-of-stock-handli...

Anyone got an ideas of getting the error to show up?

0 Likes
Highlighted
New Member
7 0 0

I guess this will remain a mystery for another 2 years.

0 Likes
Highlighted
New Member
2 0 0

Alright another 2 years later. Any reason why this is still the case?

Edit: Solved after 4 years

const axiosInstance = axios.create({
headers: {
'X-Requested-With': 'XMLHttpRequest',
},
});

Axios doesnt use that header field by default.After including it, adding a product that doesnt have available inventory returns an error(what I wanted) even if it has line item properties.

0 Likes
Highlighted
New Member
2 0 0

I'm the OP but I don't have access to the original account in order to mark this post as resolved. However here is the solution.

 

I was using Axios to make the AJAX request. Axios by default doesn't have 'X-Requested-With': 'XMLHttpRequest' included in the header, whereas jQuery does, which is why i'm assuming most people have never really run into this issue.

 

const axiosInstance = axios.create({
headers: {
'X-Requested-With': 'XMLHttpRequest',
},
});

 

After including the field in the header, the POST request fails (which is what I wanted) even after sending line items properties as part of the params.

 

Source: Wikipedia

X-Requested-WithMainly used to identify Ajax requests. Most JavaScript frameworks send this field with value of XMLHttpRequest
0 Likes