Discount Not Applying Immediately to Newly Created Products Added to Cart

TL;DR: Using the GraphQL Admin API to create products on the fly. When added to the cart via JS, discounts tied to a dynamic collection (which should include the new product) aren’t applied until the cart is changed again. Need a workaround to make the discount apply immediately.

Description:

I am using the GraphQL Admin API to create products on demand based on customer actions in the storefront. The product creation is triggered via JavaScript running directly in the storefront. After a product is successfully created via the API, the same JavaScript code adds the newly created product to the customer’s cart.

The issue I’m encountering is with discounts. We have discounts configured to apply to a dynamic collection. This collection is set up to automatically include new products, so newly created products should be eligible for the discount.

However, the discount is not applied immediately when the product is added to the cart. The discount only appears to be applied after a subsequent change to the cart (e.g., quantity change, removal of an item, etc.). The customer therefore does not see the discounted price when the item is initially added, which is causing confusion and impacting the user experience.

Steps to Reproduce:

  1. Customer performs an action in the storefront that triggers product creation via the GraphQL Admin API.
  2. JavaScript in the storefront adds the newly created product to the cart.
  3. Observe the cart: the discount associated with the dynamic collection is not applied to the newly added product.
  4. Make any change to the cart (e.g., change quantity of any item, remove an item).
  5. Observe the cart: the discount is now correctly applied to the newly added product.

Expected Behavior:

The discount should be applied to the newly created product immediately upon being added to the cart, as the product is automatically included in the dynamic collection to which the discount applies.

Current Behavior:

The discount is only applied after a subsequent cart modification. (Limitation of the Shopify platform according to the Shopify Support Team)

Impact:

This delay in discount application creates a poor user experience. Customers expect to see the discounted price immediately. The current behavior makes it appear as if the discount is not working or that the newly created product is not eligible for the discount.

Question:

Is there a known workaround or solution to ensure that discounts are applied immediately to newly created products added to the cart in this scenario? Any insights or suggestions would be greatly appreciated.

May i ask about your 2 step?
Is your JS code using an AJAX request from Shopify AJAX in your theme code, or is it using the Storefront API to add products to the cart?

and after you add the product to your cart?Have you trigger the cart update or fetch the cart explicitly ?

It uses the AJAX api

After the cart is updated i redirect to the cart page, from the page in which the product is added (e.g. Product Detail page)

Have you update the cart or do some logic after the response back?

The cart is updated via the reload to the /cart page.

  1. The procedure is call the AJAX add to cart api.
  2. Wait for the response and then redirect to the cart.
  3. The product is in the cart, the discount is not applied

After this i don’t update the cart via code anymore.

From my testing it takes about 10-15 seconds for the discount to apply to the created product, but it doesn’t apply the discount directly by reloading the /cart page without modifying the cart

You mentioned, “From my testing, it takes about 10-15 seconds for the discount to apply to the created product.” I’m wondering why it takes so long for the discount to apply to the cart. I suspect that when the cart is first loaded, the system is in the process of recalculating the total fee and applying the discount to the cart, but this process isn’t completed immediately.

My bad i should have clarified, the 10-15 seconds are the time it takes for the discount to apply. But it does not show up automatically without user input.

The “wait” would be the part between “Make any change to the cart” and “Observe the cart”. Where I modify the cart, to try and get the discount to apply automatically.

The problem from what I can think is that the product is not added to the collection (on which the discount is based on) before the product is added to the cart.