Applying a free product coupon to the cheapest product in checkout

davcogdavcog Member
in Help edited February 2013

I have a store where I want to setup a coupon that gives them a free product.

So for example if a customer adds product A to their cart and then puts in the coupon code it will subtract the cost of product A.

If a customer adds two or more of product A to their cart and then puts in the coupon code it will subtract the cost of a single product A.

If a customer adds 2 of product A and 3 of product B and then puts in the coupon code it will work out the single price of product A and product B and subtract the cheapest.

In the example above product A and Product B are both in the same product category.

Assuming my coupoun is named "free product", this is what i've got so far for javascript in my checkout template:
<script type="text/javascript">
jQuery(document).ready(function() {

  if (fc_json.hasOwnProperty('coupons')) {
  	jQuery.each(, function(i, coupon){
		if( product/i) > -1) {
			//Code logic in here
  //Update Coupon

What I don't know how to do is access the product prices using javascript and how to update the coupon and total. Any help on this would be greatly appreciated.

  • fc_adamfc_adam FoxyCart Team

    Unfortunately what you're trying to achieve there is not currently possible with coupons... you can't dynamically change the set up of a coupon using javascript, or set a coupon to discount the lowest out of two products. It would simply discount by an average of the price of the two.

    One approach you could take is set up the coupon with no actual discount - just one that applies to the cart. Then, using javascript, you'd see what products are in the cart that the discount applies to - remove the cheapest and re-add it with a product discount to make one of them free. You'd need to check on every load of the cart, as if someone removes the cheapest product - or adds a cheaper product you'll need to modify the products again to remove the discount from the now more expensive product and add it to the cheaper one.

    There would be a bit involved in that process - but that would be the best way to achieve it currently. We do have plans to expand out coupon functionality to allow for complex logic like this - but currently it's not possible natively.
  • Hi Adam,

    Thanks for the reply. The custom javascript approach is fine by me, I kind of assumed that would be the case. Could you point me in the right direction with the javascript? I know already how to loop through the coupons and find out if the correct coupon is present (see previous comment) however I don't know how to:

    search through the products in the cart and find the ones in the correct category.
    remove the cheapest
    re-add it with a product discount.

    We have a beta launch of the site on Monday so any help on this would be really helpful.
  • fc_adamfc_adam FoxyCart Team

    Your best bet will be hitting up the jsonp page here: - that will give you an idea of what the json cart object looks like and how to add/remove products as well.

    There are also some snippets here that might give you some starting points as well:
Sign In or Register to comment.