Solved

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

MilesLawrence
Tourist
12 1 4

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)

MilesLawrence
Tourist
12 1 4

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 5 (5)

MilesLawrence
Tourist
12 1 4

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.

chadhart
Visitor
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.

mktmgr
Visitor
1 0 0

Could you share the code use to accomplish this? 
I am looking to do something similar. I would like to add a product to the customer's cart if they use a discount code with a specific prefix.  I could also have it iterate through a list of codes that are prepared with the corresponding product however in my case, the item is not free, it is a donation product for $5.

Mkt Mgr
nikz35
Shopify Partner
78 0 23

Can please share the setups and codes ?

BVeatch
Shopify Partner
7 0 0

Hi @MilesLawrence ! Can you also connect with me on how you achieved this?