Hi Guys,
I have the following block of code to update order totals when either one of two classes change. Can someone show me how I can split this into a function so I can call the same code under other circumstances?
$('.update-subtotal input.number').change(function(){
if (!$(this).hasClass('quantity')) $(this).val(($(this).val()*1).toFixed(2));
var parent = $(this).parents('.update-subtotal:first');
var quantity = $(parent).find('input.quantity').val();
var lineprice = $(parent).find('input.lineprice').val();
var target = $(parent).find('input.subtotal:first');
var value = (quantity*lineprice).toFixed(2);
$(target).val(value);
var subtotal = 0;
$('input.subtotal').each(function(){
var value = ($(this).val()*1);
subtotal += value;
});
var discount = $('#discount').val();
discount = discount * 1;
var type = $('#discount_type').val();
if (type == "p") {
if (discount > 100) {
$('#discount').val("100");
discount = 100;
}
discount = (discount / 100) * subtotal;
$('#discount_percent').html("%");
} else {
$('#discount_percent').html("");
}
$('#subtotal').val(subtotal.toFixed(2));
var shipping = $('#shipping').val();
var tax = 0;
$('.update-subtotal input.tax').each(function(){
var tax_amount = $(this).val();
tax += tax_amount*1;
});
var total = subtotal*1 - discount + shipping*1 + tax*1;
$('#total_tax').val(tax.toFixed(2));
$('#total').val(total.toFixed(2));
});