Stock validation doesn't trigger adding to cart without the X-Requested-With: XMLHttpRequest parameter

I’d like to add a validation that block adding to cart more items than what’s available in stock for a given product. The cart-checkout-validation input doesn’t seem to allow to retrieve the in stock quantity of a specific product, see ProductVariant.

Any way to perform this validation?

Hi Alberto,

It doesn’t look like this is currently possible with cart validation functions - but I’ve raised this to the product team. Is there a reason why you can’t rely on the native inventory management to block add to cart when there’s not enough of a variant? eg: https://screenshot.click/27-30-6yic1-oc8gt.mp4

Hello @Liam-Shopify,

Thanks for the answer. I actually solved this by using the native validation, which wasn’t triggered on our theme because we were using a custom call to the add-to-cart API.

We discovered that, calling the routes.cart_add_url endpoint without the X-Requested-With: XMLHttpRequest header, which is used in Dawn here, that validation is not triggered and the add to cart always succeeded.

I couldn’t find any documentation about this behavior, I hope this will be helpful for others.

EDIT: I changed the title and moved this ticket into the proper category.

1 Like

Glad you figured this out Alberto - and thanks for updating the topic!