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

Solved
Highlighted
Tourist
12 1 0

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!

0 Likes
Highlighted
Tourist
12 1 0

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.

0 Likes