Anyone been successful adding a Stripe Coupon to Create Checkout Session

I'm trying to add a coupon to a Create Checkout Session in the Diiscount field. I assumed I just needed to feed it the coupon id, but when I do, I get an invalid array error.

So I'm like, I guess it wants the whole coupon array so I use the retrieve coupon action, and feed it the whole object, still get the error. What am I exactly to put in the "Discount" field?

Thanks in advance,
Twitch

Hi @TwitchBlade,

I use coupons to offer discounts on the prices. In the Checkout Session, if you set Allow Promotion Codes flag to true, then users can redeem and apply price discounts in the Checkout Session based on the valid Coupon Codes.

Thanks for the reply, @guptast. I'm trying to add the coupon automatically when the person buys 10 or more items. Right now I'm just trying to add it to a checkout no matter how many items are purchased just to get it working. So this one won't be the kind where the user enters a coupon code.

I couldn't find the Allow Promotion Codes setting in the server action.



I haven't tried by applying the coupon automatically. I don't know how the coupon code can be applied conditionally based on quantity.

Allow Promotion Codes is the 3rd setting from the top in the checkout session.

Thank you, I must be blind, or my coffee hasn't kicked in yet haha. Totally missed that setting. And I looked more than once. I know how to get the coupon to get added automatically, I'm just trying to get the coupon to add into the session at all right now. Turning on the Allow Promotion Codes just shows the customer code enter field. My coupon does not use the customer-facing coupon codes setting. So I imagine to add mine to the checkout, it needs to be entered in the Discounts field some way.

Thanks again for the replies. This is the last thing on my to-do list for this site at the moment. But then again, with us developers, there's always something on the to-do for a site haha.

-Twitch

Wait a minute. Just noticed in my screenshot of the discount field. that it appears to be asking for the code in this field. Guess I'll turn on that feature and see if that works.

Nope that didn't work. Also, you can't put something in the discounts field and set allow promotion codes to true at the same time. It throws an error telling you so.

WhooHooo, finally figured out which type of array it was looking for. To apply a coupon automatically at checkout put this in the Discounts field as code.

[{coupon: 'idOfCoupon'}]

2 Likes

I'm glad that it's working for you and thank you for showing how to apply the discount coupon code automatically.

1 Like

Hi @TwitchBlade

I'm trying to get a discount coupon working and hit upon this thread. Can you give a little more info on what idOfCoupon is? Is it a session variable? From datastore? From the database? Something else?

I want to be able to have a coupon code in the database which contains the expiry date and percentage discount value so that it can be entered at checkout. This thread seems to indicate that's pretty much what you've done but I can't work out the finer details to actual implement it.

Any assistance you can give would be very much appreciated.

Cheers, Jon.

Hey @sitestreet, replace where I have idOfCoupon with the id of the coupon you wish to add. You can only add one at a time from what I can tell even though it's an array. Although I don't think I tried to add more to test it out.

The id can be found in the Stripe dashboard under Product Catalog-->Coupons then click on the Coupon. The id will be in the right top corner or in the details section.

1 Like

Thanks @TwitchBlade

So the coupon is set up within Stripe? I can't have the coupon details in my site database?

I will give that a go. For now I only need one coupon so that should be fine but it's not ideal in the future if more coupons are needed.

I'll let you know how I get on :+1:

I created the coupon in Stripe, set the ‘allow promotion codes’ to true and it works like a charm. Frustrated I spent ages adding coupons to the site when this took about a minute!

Would love to be able to manage the coupons in the site rather than in Stripe, though.

Thanks again @TwitchBlade

1 Like

For some of my stripe stuff, I store some of the info in my site database so I don't have to retrieve it from stripe each time and for easier searching/filtering, and I always store the stripe id whether it's a product or price or coupon in my database so I can easily retrieve the rest of the info from stripe should I need it. I also, haven't been able to figure out how to enter the coupon id using a set value variable to make it dynamic. It throws an error. I tried with and without single quotes [{coupon: '{{myVariable}}'}], [{coupon: myVariable}] Since I can't put the id dynamically, I have two coupons that don't use a customer entered code, they are automatically applied and in order to tell my checkout session which one to use based on how any items they are purchasing or another condition, I have to use two different checkout sessions with the different coupon ids: