<script>
jQuery(document).ready(function($) {
// Function to format numbers with commas for thousands
function numberWithCommas(x) {
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
}
// Function to calculate and update the total price
function calculateTotal() {
var total = 0;
$('input[name="item_factura_price[]"]').each(function() {
var price = parseFloat($(this).val()) || 0;
var qtySelector = $(this).closest('.row-postmeta-items').find('input[name="item_factura_qty[]"]');
var qty = parseInt(qtySelector.val()) || 0;
total += price * qty;
});
// Format the total with commas and update the text
$('#totalPrice').text(numberWithCommas(total.toFixed(2)));
}
// Calculate total when the page loads
calculateTotal();
// Recalculate total whenever quantity or price fields change
$(document).on('change', 'input[name="item_factura_qty[]"], input[name="item_factura_price[]"]', function() {
calculateTotal();
});
// Also recalculate when items are added or removed
$(document).on('click', '.item-factura-add, .item-factura-remove', function() {
// Add a slight delay to allow the DOM to update
setTimeout(calculateTotal, 100);
});
});
</script>
</div>
`
`