I am having problems with applying discount codes that I've previously created using API call.
To be more specific - there is a delay time between creating discount code via API call and discount becoming available for use.
First, I create price rule using API endpoint:
Then I create discount from that same price rule using endpoint:
and then I return that discount code back to frontend, but I can't use the code for first couple of seconds or minutes.
When I look in "Discounts" section in admin panel, I can see that new discount is there, but under discount status it says "scheduled" instead of "active". It becomes active after some time. Sometimes it becomes active immediately, sometimes after few seconds, and sometimes after minute or two.
Once it becomes active I can use it normally and it works fine, but I don't know why am I getting that unknown delay?
When I'm creating price rule, I'm setting "starts_at" property to current datetime value, so I don't think that should be a problem?
My question is:
Where does that delay come from, and how to prevent that?
Solved! Go to the solution
Hello @jldevelop ,
I think a minor delay between creating a discount code and it being usable in the checkout is fairly reasonable, things will take some time to propagate/process in order for the checkout to be aware that the discount code has been activated and available for use.
With that being said though, do you have any kind of rough estimate of how often you find it taking minutes before the discount will work? I have only been able to replicate it taking ~10-15 seconds at best, which I don't think would be avoidable.
I would recommend trying to take the current time when creating your price rules and discount codes and then subtracting a minute or two to see if that helps with this issue at all, and let me know if you find that makes a difference.
This is an accepted solution.
Yeah, I thought so and that makes sense. Although, that delay varies. Sometimes it takes only 1 second, and sometimes it takes much longer (don't know how much longer, but usually it is inside of 10-15 seconds timeframe like you said). One time I needed to wait for approx 1 min, but I wasn't able to reproduce that more than once.
I think it would be good idea to have some kind of workaround for that in case shop owner wants to create discount code on the fly and apply that discount to customer's checkout right away.
Also, I think it would be useful to include "discount_status" object property in discounts API call responses, so we can check discount status periodically.
Interesting thing is that I haven't experienced that discount activation delay when I created discount code manually in shopify discounts section. Discount gets created and activated right away.
Anyway, we found a workaround. We are now pre-creating price rules and discounts, and when we have to use them, we just update price_rule's value and apply the discount code. Looks like we solved the delay issue with it. It does the job, but it would be definitely nice to have simpler and cleaner solution in the future.