jQuery(document).ready(function($) {
function syncB2BPPOMPrice() {
var qty = parseInt($('input.qty').val()) || 1;
var b2bBasePrice = 0;
var ppomFixedTotal = 0;
// 1. GET THE B2B TIERED PRICE
var $b2bTable = $('.b2bking_tiered_price_table');
if ($b2bTable.length > 0) {
$b2bTable.find('tbody tr').each(function() {
var $row = $(this);
var rangeAttr = $row.find('td').first().attr('data-range') || "";
var minQty = parseInt(rangeAttr.split('-')[0].replace(/[^\d]/g, '')) || 0;
if (qty >= minQty && minQty > 0) {
var tierValue = parseFloat($row.find('.b2bking_hidden_tier_value').val());
if (!isNaN(tierValue)) { b2bBasePrice = tierValue; }
}
});
}
// Fallback for Qty 1-24
if (b2bBasePrice === 0) {
b2bBasePrice = parseFloat($('.woocommerce-Price-amount bdi').first().text().replace(/[^\d.]/g, '')) || 0;
}
// 2. SUM PPOM FIXED PRICES
$('.ppom-fixed-price .ppom-price').each(function() {
var price = parseFloat($(this).text().replace(/[^\d.]/g, '')) || 0;
ppomFixedTotal += price;
});
// 3. CALCULATIONS
var productSubtotal = b2bBasePrice * qty;
var grandTotal = productSubtotal + ppomFixedTotal;
// 4. OVERWRITE PPOM DISPLAY (The Fix for the £2.60 Flicker)
var $baseRow = $('.ppom-product-base-price');
$baseRow.find('.ppom-label-item .ppom-price').text(b2bBasePrice.toFixed(2));
$baseRow.find('.ppom-price-item .ppom-price').text(productSubtotal.toFixed(2));
$('.ppom-total-without-fixed .ppom-price').text(grandTotal.toFixed(2));
// 5. UPDATE SUMMARY BOX
$('.ppom-total-price-display').html(
'Calculated Summary:
' +
'B2B Price: £' + b2bBasePrice.toFixed(2) + ' x ' + qty + ' = £' + productSubtotal.toFixed(2) + '
' +
'Fixed Add-ons: £' + ppomFixedTotal.toFixed(2) + '
' +
'