Created
January 3, 2014 07:12
-
-
Save ndimatteo/8234088 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('.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