This is an old revision of the document!


type:
snippet
category:
checkout
name:
Adding a coupon code field to the checkout
reference:
http://forum.foxycart.com/comments.php?DiscussionID=3018&page=1
tags:
snippets, checkout, advance
date:
2011-05-27

Adding a coupon code field to the checkout

Its has been whipped up for a user who wasn't doing shipping or tax, so that's VERY IMPORTANT TO NOTE. This code is being used by one store but for all intents and purposes consider it UNTESTED, so make sure you test it thoroughly, as it might not work with your specific settings.

FC.checkout.InitCoupon = function() {
	fc_cart_foot_discount_new = '<tr id="fc_cart_foot_discount_new"><td class="fc_col1" colspan="2"><a href="#" onclick="FC.checkout.AddCoupon(); this.blur(); return false;">Add a coupon</a></td><td class="fc_col2"><input type="text" name="coupon" id="fc_coupon" class="fc_text fc_text_short" value="" style="display:none;" /><a id="fc_coupon_apply" href="javascript:;" style="display:none;">Apply!</a></td></tr>';
 
	if (jQuery('#fc_cart_foot_discount_new').length == 0) {
		jQuery(fc_cart_foot_discount_new).insertBefore('#fc_cart_foot_shipping');
	}
 
	jQuery('#fc_coupon_apply').unbind('click').click(function(){
		coupon = jQuery('#fc_coupon').val();
		if (coupon != '') {
			FC.checkout.ApplyCoupon(coupon);
		} else {
			alert('Please enter a coupon code.');
		}
	});
}
 
FC.checkout.AddCoupon = function() {
	jQuery("#fc_coupon, #fc_coupon_apply").toggle();
	if (jQuery("#fc_coupon").is(":visible")) {
		jQuery("#fc_coupon")[0].focus();
	}
}
 
FC.checkout.ApplyCoupon = function(coupon) {
	jQuery('#fc_coupon_apply').html('Loading...');
	console.info('getJSON?');
	jQuery.getJSON('https://' + window.location.hostname + '/cart?output=json&' + FC.checkout.config.session + '&coupon=' + coupon + '&callback=?',
		function(data) {
			console.info(data);
			if (data.messages.errors.length > 0) {
				alert("We're sorry. An error occurred: " + data.messages.errors[0]);
			} else {
				console.info('Coupon: ' + data.coupons);
				FC.checkout.BuildCouponTR(data.coupons);
			}
			jQuery('#fc_coupon_apply').html('Apply!');
		}
	);
}
 
FC.checkout.BuildCouponTR = function(coupons) {
	console.info('Coupons: ' + coupons)
	fc_cart_foot_discounts = '';
	FC.checkout.config.orderDiscount = 0;
	for (coupon in coupons) {
		fc_cart_foot_discounts += '<tr class="fc_cart_foot_discount"><td class="fc_col1" colspan="2">' + coupon + ':</td><td class="fc_col2"><span class="fc_discount">' + FC.formatter.currency(coupons[coupon], true) + '</span></td></tr>';
		FC.checkout.config.orderDiscount += coupons[coupon];
	}
	jQuery(fc_cart_foot_discounts).insertAfter('#fc_cart_foot_subtotal');
	// Set the subtotal amounts
	jQuery('#discount, label[for=discount]').remove();
	if (FC.checkout.config.orderDiscount != 0) {
		discount_total = '<li class="fc_row fc_discount"><label for="discount" class="fc_pre">Discount</label><input type="text" name="discount" id="discount" class="fc_text fc_text_short fc_text_readonly" readonly="readonly" onfocus="this.blur()" value="' + FC.formatter.currency(FC.checkout.config.orderDiscount) + '" /></li>';
		jQuery(discount_total).insertAfter('li.fc_shipping_cost');
	}
	FC.checkout.updatePriceDisplay();
}
 
 
jQuery(document).ready(function(){
	FC.checkout.InitCoupon();
});

Site Tools