| 1 |
/** |
| 2 |
* Woo Checkout Payment widget behavior. |
| 3 |
* |
| 4 |
* Enhances payment methods UI and can optionally behave like an accordion. |
| 5 |
*/ |
| 6 |
(function ($) { |
| 7 |
"use strict"; |
| 8 |
|
| 9 |
const INIT_FLAG = "kaCheckoutPaymentInit"; |
| 10 |
|
| 11 |
const safeParseJson = (value) => { |
| 12 |
if (!value) return {}; |
| 13 |
try { |
| 14 |
const parsed = JSON.parse(value); |
| 15 |
return parsed && typeof parsed === "object" ? parsed : {}; |
| 16 |
} catch (e) { |
| 17 |
return {}; |
| 18 |
} |
| 19 |
}; |
| 20 |
|
| 21 |
const initAccordion = (root) => { |
| 22 |
if (!root || !root.dataset) return; |
| 23 |
if (root.dataset[INIT_FLAG] === "1") return; |
| 24 |
root.dataset[INIT_FLAG] = "1"; |
| 25 |
|
| 26 |
const isAccordion = root.dataset.kaAccordion === "true"; |
| 27 |
const iconMap = safeParseJson(root.dataset.kaIcons); |
| 28 |
const buttonMap = safeParseJson(root.dataset.kaPlaceorder); |
| 29 |
const descMap = safeParseJson(root.dataset.kaDescriptions); |
| 30 |
|
| 31 |
const methods = root.querySelectorAll('.wc_payment_method'); |
| 32 |
const placeOrder = document.querySelector('#place_order'); |
| 33 |
|
| 34 |
methods.forEach((method) => { |
| 35 |
const input = method.querySelector('input.input-radio'); |
| 36 |
const label = method.querySelector('label'); |
| 37 |
const desc = method.querySelector('.payment_box'); |
| 38 |
|
| 39 |
if (label && input) { |
| 40 |
const gid = input.value; |
| 41 |
if (iconMap && iconMap[gid]) { |
| 42 |
const icon = document.createElement('img'); |
| 43 |
icon.src = iconMap[gid]; |
| 44 |
icon.alt = ''; |
| 45 |
icon.className = 'ka-wc-payment__icon'; |
| 46 |
label.prepend(icon); |
| 47 |
} |
| 48 |
if (descMap && descMap[gid]) { |
| 49 |
let custom = method.querySelector('.ka-wc-payment__custom-desc'); |
| 50 |
if (!custom) { |
| 51 |
custom = document.createElement('div'); |
| 52 |
custom.className = 'ka-wc-payment__custom-desc'; |
| 53 |
label.appendChild(custom); |
| 54 |
} |
| 55 |
custom.innerHTML = descMap[gid]; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
if (!isAccordion || !input || !desc) return; |
| 60 |
|
| 61 |
const toggle = () => { |
| 62 |
methods.forEach((m) => { |
| 63 |
const box = m.querySelector('.payment_box'); |
| 64 |
if (box) { |
| 65 |
box.style.display = 'none'; |
| 66 |
} |
| 67 |
m.classList.remove('is-active'); |
| 68 |
}); |
| 69 |
desc.style.display = 'block'; |
| 70 |
method.classList.add('is-active'); |
| 71 |
if (placeOrder && buttonMap && buttonMap[input.value]) { |
| 72 |
placeOrder.textContent = buttonMap[input.value]; |
| 73 |
} |
| 74 |
}; |
| 75 |
|
| 76 |
input.addEventListener('change', toggle); |
| 77 |
if (input.checked) { |
| 78 |
toggle(); |
| 79 |
} else { |
| 80 |
desc.style.display = 'none'; |
| 81 |
} |
| 82 |
}); |
| 83 |
}; |
| 84 |
|
| 85 |
const init = () => { |
| 86 |
document.querySelectorAll('.ka-woo-checkout-payment').forEach(initAccordion); |
| 87 |
}; |
| 88 |
|
| 89 |
const initInScope = ($scope) => { |
| 90 |
const root = $scope && $scope[0] ? $scope[0] : document; |
| 91 |
root.querySelectorAll(".ka-woo-checkout-payment").forEach(initAccordion); |
| 92 |
}; |
| 93 |
|
| 94 |
document.addEventListener("DOMContentLoaded", init); |
| 95 |
|
| 96 |
$(document.body).on("updated_checkout", function () { |
| 97 |
// Allow re-init after WooCommerce updates. |
| 98 |
document.querySelectorAll(".ka-woo-checkout-payment").forEach((wrap) => { |
| 99 |
if (wrap && wrap.dataset) { |
| 100 |
delete wrap.dataset[INIT_FLAG]; |
| 101 |
} |
| 102 |
}); |
| 103 |
init(); |
| 104 |
}); |
| 105 |
|
| 106 |
$(window).on("elementor/frontend/init", function () { |
| 107 |
elementorFrontend.hooks.addAction( |
| 108 |
"frontend/element_ready/woo_checkout_payment.default", |
| 109 |
function ($scope) { |
| 110 |
initInScope($scope); |
| 111 |
} |
| 112 |
); |
| 113 |
}); |
| 114 |
})(jQuery); |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|