Skip to content

Instantly share code, notes, and snippets.

@carlitoescobar
Forked from prayas-sapkota/Quantity Discounts
Created December 10, 2020 18:22
Show Gist options
  • Save carlitoescobar/4bc78fb08e8c2a709ffd37a74bbcedcc to your computer and use it in GitHub Desktop.
Save carlitoescobar/4bc78fb08e8c2a709ffd37a74bbcedcc to your computer and use it in GitHub Desktop.
(function($){
$.fn.quantityDiscounts = function(options) {
// set up default options
var defaults = {
discountColor: '#FF0000',
discounts: [
{ limit: 174, discount: .75, text: "25% Discount Applied"},
{ limit: 139, discount: .80, text: "20% Discount Applied"},
{ limit: 99, discount: .85, text: "15% Discount Applied"},
{ limit: 79, discount: .90, text: "10% Discount Applied"},
{ limit: 59, discount: .95, text: "5% Discount Applied"}
],
test: "Test"
};
var options = $.extend({}, defaults, options);
return this.each(function(){
var el = this;
var item = this.id.replace('field','');
var itemval;
var discount = 1;
var newprice;
var curprice;
$(el).on('keyup', '#ginput_quantity'+item, function(){
// Set data element to keep original price in place
if( $('#input'+item, el).data('original') === undefined ){
$('#input'+item, el).data('original', cleanPrice($('#ginput_base_price'+item, el).val()));
}
// More resets
itemval = $(this).val();
discount = 1;
discountText = '';
// Find discount and set text
$.each(options.discounts, function(){
if( itemval > this.limit ){
discount = this.discount;
discountText = this.text;
return false;
}
});
// Discount display
var discountDisplay = $('<span></span>').addClass('discountText').attr('id', 'discount'+item).css({'color': options.discountColor, 'padding-left': '10px'});
curprice = $('#input'+item, el).data('original');
newprice = discount * curprice;
// Update fields with new price
$('#ginput_base_price'+item, el).val(displayPrice(newprice));
$('#input'+item, el).text(displayPrice(newprice));
if( !$('#discount'+item, el).length ){
$('#ginput_quantity'+item, el).after(discountDisplay);
}
// Add discount notice
$('#discount'+item, el).text(discountText);
});
});
function cleanPrice(str){
return str.replace("$", "");
}
function displayPrice(str){
return '$'+parseFloat(str).toFixed(2);
}
};
})(jQuery);
(function($){
$.fn.quantityDiscounts = function(options) {
// set up default options
var defaults = {
price_field: '#field_3_66',
amounts: [
{ limit: 100, amount: 65 },
{ limit: 40, amount: 80 },
{ limit: 20, amount: 90 },
{ limit: 10, amount: 100 }
]
};
var options = $.extend({}, defaults, options);
return this.each(function(){
var el = this;
var item = this.id.replace('field','');
var itemval;
var amount = 1;
var newprice;
var curprice;
$(el).on('keyup load', '#input'+item, function(){
// Set data element to keep original price in place
if( $('#input'+item, el).data('original') === undefined ){
$('#input'+item, el).data('original', $(options.price_field+' input:checked').val());
}
// Get current value of checked field
curprice = $(options.price_field+' input:checked').val();
// Update pricing with pipe
curprice = curprice.substring(0, curprice.lastIndexOf('|') + 1);
// More resets
itemval = $(this).val();
amount = 1;
// Find amount and set text
$.each(options.amounts, function(){
if( itemval >= parseInt(this.limit) ){
amount = parseFloat(this.amount);
return false;
}
});
$(options.price_field+' input:checked').val(curprice+amount);
});
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment