FROM CACHE - en_header

422 error during /cart/add.js

szuke
Tourist
4 0 1

I'm probably like most partners and I find myself lurking around these forums more than posting in them. I thought I'd share a minor fix that might help others. 

When performing an ajax post to /cart/add.js, I was getting a 422 error. This seems easy now, but it still took time to identify. 

 

The issue is, when creating variants on-the-fly, I wasn't setting the inventory_policy value. The default for this value is 'deny'. This means that I would create a variant, the initial inventory would be zero, then the add to cart would fail because it's not allowed to be sold when there isn't inventory. In my case, I wanted to create the variant with an inventory_policy of 'continue' so that the variant could be added to the cart.

 

Like I said, seems easy now. 

 

 

Replies 7 (7)
niko-paris
Tourist
8 0 1

Hello @szuke ,

 

thank you for sharing, would you mind to share your code also ? It could help to see exactly what needs to be done 🙂

Kind regards,

Nicolas

szuke
Tourist
4 0 1

This is an example from a python request, but the idea remains the same:

 

payload = "{\"variant\": {\"option1\": \"" + str(timestamp) + "\",\"price\": \"" + price + "\",\"inventory_policy\":\"continue\"}}"

 

adambrandt
New Member
3 0 0

Hi has anyone solved the Cart Error(422) error display? out of stock initiates this error on trying to add another out of stock item.

I'm not a coder so pls be gentle:-)

sillycube
Shopify Partner
701 16 106

Thanks @szuke 

Your solution is useful. I am doing similar things with python. It works great now.

BYOB - Build Your Own Bundles, SPO - SEO App to research keywords & edit social link preview
Arhum
Excursionist
23 2 7

Hi, Thanks for sharing with us.

I am working on similar things and sending inventory_policy as continue. Changes has been reflected i can see in response but when i goto checkout it still shows "OUT OF STOCK".

if you anything about this please let know. Thanks 

 

sillycube
Shopify Partner
701 16 106

I tried to create a new variant and add it to the cart immediately. When I add the new variant to the cart, the new variant price, product image, options are gone as the 1st line item. The 2nd one is a working example. After I refresh the browser, it is showing the correct result again. How to resolve it? Any one?

sillycube_0-1622202745849.png

 

BYOB - Build Your Own Bundles, SPO - SEO App to research keywords & edit social link preview
sillycube
Shopify Partner
701 16 106

@szuke 

If you set inventory policy to continue, will it cause overselling? Seems like it's creating another problem 

BYOB - Build Your Own Bundles, SPO - SEO App to research keywords & edit social link preview