Stop discount code being applied twice

Hi there, I have a coupon code that I’m allowing users to apply at the checkout. This is fine, and the button will only work once while you’re on that page as I set a state on the checkout page. The ‘enter’ button only works when the page’s state is empty.

However, the problem is that if you navigate away from that page, i.e. back to the basket and then to the checkout again, the checkout page loses its state and so the discount code is able to be applied for a second time… and a third, fourth etc if you realise you just need to go back and forward again in your browser!

if the codes are stored in your database, create a number field as a counter, if it’s used increase the counter by 1, and restrict / deny codes that have a count of > 0 . The count increase can be done on the checkout workflow so if they enter the code, but don’t complete purchase, it would still be valid another time.


Q: The checkout is for logged users or non-logged?
A: If it’s logged, I would create a separated data type for “coupons,” add a type-coupon field in the checkout process and add the entry to the linked checkout.

If they are not logged in, or non-logged users also can buy without logging in, I would be using cookies or browser storage instead of states.