Add a new Currency field to the form and give it the grandTotal CSS class.
The total field will now only show the table subtotal. The grand total field will show the total plus shipping and tax.
Replace the JavaScript from the example with the following:
$(document).ready(function () {
$('.cf-table-block').on('blur', 'input', sumtotal);
$('.tax').on('blur', 'input', sumtotal);
$('.shipping').on('blur', 'input', sumtotal);
function sumtotal() {
var sum = 0;
$('.cf-table-block tbody tr').each(function () {
var s = 0;
s = parseNumber($(this).find('.price input').val()) * parseNumber($(this).find('.quantity input').val());
$(this).find('.subtotal input').val((s).toFixed(2));
sum += s;
});
$('.total input').val((sum).toFixed(2));
$('.grandTotal input').val((sum + parseNumber($('.tax input').val()) + parseNumber($('.shipping input').val())).toFixed(2));
}
function parseNumber(n) {
var f = parseFloat(n); //Convert to float number.
return isNaN(f) ? 0 : f; //treat invalid input as 0;
}
});
Note: This code further enhances the online help example by restricting the values to two decimal places using the .toFixed() method.
To add a little more explanation: the sum variable keeps the sum of each table row. In the version of this code shown in the online help, this value was added to the shipping and tax amount before being placed into the total field.
To change the code, we placed only the sum value into the total field, and added a new grand total field that takes the total amount plus shipping and tax.