I would like to create an order discount with a code that is created offline by our app in a flow that is triggered when a user clicks a button on a checkout extension. The use case is a loyalty program, where a user exchange loyalty points for a discount. So the button in the checkout extension calls our backend which uses the offline access to the store to create a discount code. Then, I want to have this discount code to be applied to the cart, so that the customer will get the discount. I heard that this can be done using Shopify Functions, but I can't find any way to do it, since the function logic only runs after the discount code is applied to cart. The checkout extension obviously can't do it since there's no "applyDiscountCode" API for the extensions. Can this be done using Functions?
You're correct that Functions can't mutate the cart, they can only apply discounts based on the cart state. Checkout extensions can't yet apply discount codes either, but I am told this is in their backlog.
Is there a user flow for your app that would allow you to use discount code permalinks instead?
https://<shop url>/discount/<discount code>?redirect=<url>
We are developing a small user flow that uses permalinks, but our main flow is creating discount codes at checkout and adding them to the cart.