Skip to content

Instantly share code, notes, and snippets.

@ndimatteo
Created January 3, 2014 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndimatteo/8234088 to your computer and use it in GitHub Desktop.
Save ndimatteo/8234088 to your computer and use it in GitHub Desktop.
$('.add-coupon').each(function() {
$(this).click(function() {
var couponVal = $('.coupon', this).val();
$('span', this).text('Applying');
$.getJSON('/check-coupon', {
coupon: couponVal
}, function(data) {
// Invalid
if (!data.valid) {
$('.add-coupon span').text('Apply');
alert('INVALID CODE');
// Inactive & Valid
} else if (!data.active){
$('.add-coupon span').text('Apply');
alert('INACTIVE CODE');
// Active & Valid
} else {
// Percent
if (data.percent !== null) {
$('.applied-coupon').val(data.percent+'% OFF');
// Amount
} else if (data.amount !== null) {
$('.applied-coupon').val('$'+(data.amount/100)+' OFF');
}
// Repeating?
if (data.duration === 'repeating') {
$('.applied-coupon').val($('.applied-coupon').val()+' for '+data.months+' month(s)');
} else if (data.duration === 'forever') {
$('.applied-coupon').val($('.applied-coupon').val()+' forever');
}
$('.coupon, .add-coupon').hide();
$('.applied-coupon, .remove-coupon').show();
$('.add-coupon span').text('Apply');
}
}
);
});
});
$('.remove-coupon').click(function() {
$('.coupon').val('');
$('.coupon, .add-coupon').show();
$('.applied-coupon, .remove-coupon').hide();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment