1/** 2 * Product Detail - Voucher Discount Recalculation 3 * 4 * Handles dynamic voucher discount recalculation when variant price changes 5 * Supports both PERCENTAGE and FIXED_AMOUNT voucher types 6 */ 7 8(function() { 9 'use strict'; 10 11 /** 12 * Calculate voucher discount based on type and price 13 * 14 * @param {string} type - Voucher type: 'PERCENTAGE' or 'FIXED_AMOUNT' 15 * @param {number} value - Voucher value (percentage or fixed amount) 16 * @param {number} price - Current variant price 17 * @param {number} maxDiscount - Maximum discount for percentage vouchers (0 = no limit) 18 * @return {number} Calculated discount amount 19 */ 20 function calculateVoucherDiscount(type, value, price, maxDiscount) { 21 if (type === 'PERCENTAGE') { 22 // Calculate percentage discount 23 let discount = (price * value) / 100; 24 25 // Apply max discount cap if set 26 if (maxDiscount > 0 && discount > maxDiscount) { 27 discount = maxDiscount; 28 } 29 30 return Math.round(discount); 31 } else { 32 // FIXED_AMOUNT - discount doesn't change with price 33 return Math.round(value); 34 } 35 } 36 37 /** 38 * Format number as Rupiah without 'Rp' prefix 39 * 40 * @param {number} num - Number to format 41 * @return {string} Formatted number (e.g., "20.000") 42 */ 43 function formatNumber(num) { 44 return Math.round(num).toString().replace(/\B(?=(\d{3})+(?!\d))/g, '.'); 45 } 46 47 /** 48 * Update voucher button discount label 49 * 50 * @param {number} newPrice - New variant price 51 */ 52 function updateVoucherDiscount(newPrice) { 53 const voucherBtn = document.getElementById('btn-buy-voucher'); 54 if (!voucherBtn) return; 55 56 // Get voucher data from button attributes 57 const voucherType = voucherBtn.dataset.voucherType; 58 const voucherValue = parseFloat(voucherBtn.dataset.voucherValue) || 0; 59 const voucherMax = parseFloat(voucherBtn.dataset.voucherMax) || 0; 60 61 if (!voucherType || voucherValue === 0) return; 62 63 // Calculate new discount 64 const newDiscount = calculateVoucherDiscount(voucherType, voucherValue, newPrice, voucherMax); 65 66 // Update the discount label 67 const saveLabel = voucherBtn.querySelector('.bv-save'); 68 if (saveLabel) { 69 saveLabel.textContent = '-Rp' + formatNumber(newDiscount); 70 } 71 72 // Also update mobile FAB voucher display if exists 73 updateMobileFabVoucher(newDiscount); 74 } 75 76 /** 77 * Update mobile FAB voucher value 78 * 79 * @param {number} discountAmount - New discount amount 80 */ 81 function updateMobileFabVoucher(discountAmount) { 82 const fab = document.getElementById('pd-mobile-fab'); 83 if (fab) { 84 fab.dataset.voucherValue = discountAmount.toString(); 85 } 86 } 87 88 /** 89 * Listen to variant change events and recalculate discount 90 */ 91 function initVoucherRecalculation() { 92 window.addEventListener('pd-variant-changed', function(e) { 93 const detail = e.detail || {}; 94 const newPrice = detail.price || 0; 95 96 if (newPrice > 0) { 97 updateVoucherDiscount(newPrice); 98 } 99 }); 100 } 101 102 // Initialize when DOM is ready 103 if (document.readyState === 'loading') { 104 document.addEventListener('DOMContentLoaded', initVoucherRecalculation); 105 } else { 106 initVoucherRecalculation(); 107 } 108 109})();
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.