Adding a product to cart using a discount code in the checkout page

Solved
MilesLawrence
Tourist
12 1 3

Hi all, part of the functionality of the store we're building is the feature to add (free) specific products to the cart based on a discount code they enter at the checkout page. I can't seem to find a reliable way to do this, but maybe some of you can point me in the right direction or help me get started.

Appreciate any advice you all can offer, cheers!

Accepted Solution (1)

Accepted Solutions
MilesLawrence
Tourist
12 1 3

This is an accepted solution.

I was able to come up with a solution using the Cart API and some javascript on the checkout.

Basically, when the apply discount button is clicked, it iterates through a list of discount codes prepared with corresponding free gifts. If the text in the discount code field matches one of the discount codes in the list, it calls POST /cart/update.js with the variant id of the product and quantity set to 1 (so entering the same code multiple times won't duplicate).

Removing the product was simply iterating each discount tag and watching their button clicks, then matching the discount text with the list again and removing the associated product ID from the cart.

View solution in original post

Replies 2 (2)
MilesLawrence
Tourist
12 1 3

This is an accepted solution.

I was able to come up with a solution using the Cart API and some javascript on the checkout.

Basically, when the apply discount button is clicked, it iterates through a list of discount codes prepared with corresponding free gifts. If the text in the discount code field matches one of the discount codes in the list, it calls POST /cart/update.js with the variant id of the product and quantity set to 1 (so entering the same code multiple times won't duplicate).

Removing the product was simply iterating each discount tag and watching their button clicks, then matching the discount text with the list again and removing the associated product ID from the cart.

View solution in original post

chadhart
New Member
1 0 0

@MilesLawrence can you connect me with the developers that built that functionality for you? I'm needing that solution on my store as well.