422 error during /cart/add.js

Highlighted
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. 

 

 

0 Likes
Highlighted
New Member
6 0 0

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

0 Likes
Highlighted
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\"}}"

 

1 Like