Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Auto discount prices based on quantities in Gravity Forms. Uses a percentage based approach.
(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);
@rdueck

This comment has been minimized.

Copy link

commented Nov 29, 2016

this is exactly what I've been looking for. I tried putting it in my functions.php but it errors out. Is there another place that it should be placed? or should it be formatted differently?

@amingholami

This comment has been minimized.

Copy link

commented Nov 27, 2017

It seems above code is a javascript jQuery.
you need to put it to a .js file.
but I think it can be hacked because it's a frontend code, what's your idea?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.