Hi everyone,
I’m facing an intermittent issue when creating a product variant dynamically via the Admin API and immediately adding it to the cart using /cart/add.js.
What I’m doing
-
Backend creates a new product + variant (used for add-ons / custom products).
-
Backend sends the newly created
variant_idto the frontend. -
Frontend calls:
POST /cart/add.js -
Sometimes it works, but very often it fails with
422 Unprocessable Entity.
Important observations
-
GET /variants/{variant_id}.jsonreturns 200 OK -
The variant clearly exists and is readable
-
Product is published to Online Store
-
Variant has price, shipping enabled, and no inventory restrictions
-
Adding a delay before
/cart/add.jssometimes fixes it- but the delay is inconsistent (1s, 4s, 6s, etc.)
-
Retrying aggressively leads to 429 / Cloudflare challenge responses
What makes this confusing
-
variant.jsonbeing accessible suggests the variant exists -
But
/cart/add.jsstill rejects it with 422 -
This looks like a propagation / cart-readiness delay
-
There doesn’t seem to be a reliable signal to know when a variant becomes “cart eligible”
What I’ve already tried
-
Fixed delays before adding to cart
-
Retry logic with backoff
-
Checking variant availability via
/variants/{id}.json -
Ensuring product is published to Online Store
-
Ensuring variant is not draft and has valid price
None of these are reliable in production.
Questions
-
Is this a known limitation of adding newly created variants to cart via Ajax?
-
Is there a supported way to detect when a variant becomes cart-ready?
Use case
-
Custom products / add-ons
-
Variant must be created dynamically
-
Immediate checkout experience expected
Expected behavior
If a variant:
-
exists,
-
is published to Online Store,
-
and is readable via public endpoints,
it should either:
-
be addable to cart, or
-
provide a deterministic way to know when it becomes addable.
Any guidance from Shopify staff or developers who’ve handled this would be really appreciated.
Thanks in advance!