| 1 |
/** |
| 2 |
* Woo Cart Totals widget behavior. |
| 3 |
* |
| 4 |
* Handles applying/removing coupons via AJAX and refreshing cart fragments. |
| 5 |
* Exposes a global initializer for other cart-related widgets. |
| 6 |
*/ |
| 7 |
(function ($) { |
| 8 |
"use strict"; |
| 9 |
|
| 10 |
const FLAG = "kaCartTotalsInit"; |
| 11 |
|
| 12 |
const KACartTotals = window.KACartTotals || (() => { |
| 13 |
const toggleBody = (wrap) => { |
| 14 |
const body = wrap.querySelector('.ka-cart-coupon__body'); |
| 15 |
if (!body) return; |
| 16 |
const isHidden = body.hasAttribute('hidden'); |
| 17 |
if (isHidden) { |
| 18 |
body.removeAttribute('hidden'); |
| 19 |
} else { |
| 20 |
body.setAttribute('hidden', ''); |
| 21 |
} |
| 22 |
}; |
| 23 |
|
| 24 |
const setNotice = (wrap, text) => { |
| 25 |
const notice = wrap.querySelector('.ka-cart-coupon__notice'); |
| 26 |
if (notice) { |
| 27 |
notice.textContent = text || ''; |
| 28 |
} |
| 29 |
}; |
| 30 |
|
| 31 |
const applyFragments = (data) => { |
| 32 |
if (data.totals_html !== undefined) { |
| 33 |
document.querySelectorAll('.ka-woo-cart-totals').forEach((totals) => { |
| 34 |
totals.innerHTML = data.totals_html || ''; |
| 35 |
// The listeners lived on the elements just replaced, while the |
| 36 |
// "already bound" flag sits on the wrapper and survived - so the |
| 37 |
// coupon form worked exactly once per page load. |
| 38 |
if (totals.dataset) { |
| 39 |
delete totals.dataset[FLAG]; |
| 40 |
} |
| 41 |
bind(totals); |
| 42 |
}); |
| 43 |
} |
| 44 |
if (data.cart_html && document.querySelector('.ka-cart-table')) { |
| 45 |
document.querySelector('.ka-cart-table').innerHTML = data.cart_html; |
| 46 |
if (window.KACartTable && typeof window.KACartTable.init === "function") { |
| 47 |
window.KACartTable.init(document); |
| 48 |
} |
| 49 |
} |
| 50 |
if (data.cross_sells_html !== undefined) { |
| 51 |
document.querySelectorAll('.ka-woo-cart-cross-sells').forEach((cross) => { |
| 52 |
cross.innerHTML = data.cross_sells_html || ''; |
| 53 |
}); |
| 54 |
if (window.KACartCrossSells && typeof window.KACartCrossSells.init === "function") { |
| 55 |
window.KACartCrossSells.init(); |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
document.body.dispatchEvent(new Event('wc_fragments_refreshed')); |
| 60 |
}; |
| 61 |
|
| 62 |
const sendCoupon = (wrap, mode, code) => { |
| 63 |
const ajaxUrl = wrap.dataset.ajaxUrl; |
| 64 |
const nonce = wrap.dataset.nonce; |
| 65 |
if (!ajaxUrl || !nonce) return; |
| 66 |
|
| 67 |
const body = new URLSearchParams(); |
| 68 |
body.append('action', 'ka_cart_coupon'); |
| 69 |
body.append('nonce', nonce); |
| 70 |
body.append('mode', mode); |
| 71 |
body.append('coupon', code); |
| 72 |
|
| 73 |
wrap.classList.add('loading'); |
| 74 |
|
| 75 |
fetch(ajaxUrl, { |
| 76 |
method: 'POST', |
| 77 |
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }, |
| 78 |
body: body.toString(), |
| 79 |
credentials: 'same-origin', |
| 80 |
}) |
| 81 |
.then((res) => res.json()) |
| 82 |
.then((res) => { |
| 83 |
if (res && res.success) { |
| 84 |
applyFragments(res.data || {}); |
| 85 |
} |
| 86 |
setNotice(wrap, (res && res.data && res.data.notices) || ''); |
| 87 |
}) |
| 88 |
.catch(() => setNotice(wrap, '')) |
| 89 |
.finally(() => wrap.classList.remove('loading')); |
| 90 |
}; |
| 91 |
|
| 92 |
const bind = (wrap) => { |
| 93 |
if (!wrap || !wrap.dataset) return; |
| 94 |
if (wrap.dataset[FLAG] === "1") return; |
| 95 |
wrap.dataset[FLAG] = "1"; |
| 96 |
|
| 97 |
const toggle = wrap.querySelector('[data-ka-coupon-toggle]'); |
| 98 |
const input = wrap.querySelector('.ka-cart-coupon__input'); |
| 99 |
const applyBtn = wrap.querySelector('.ka-cart-coupon__apply'); |
| 100 |
const removeBtn = wrap.querySelector('.ka-cart-coupon__remove'); |
| 101 |
|
| 102 |
if (toggle) { |
| 103 |
toggle.addEventListener('click', () => toggleBody(wrap)); |
| 104 |
} |
| 105 |
if (applyBtn && input) { |
| 106 |
applyBtn.addEventListener('click', () => sendCoupon(wrap, 'apply', input.value)); |
| 107 |
} |
| 108 |
if (removeBtn && input) { |
| 109 |
removeBtn.addEventListener('click', () => sendCoupon(wrap, 'remove', input.value)); |
| 110 |
} |
| 111 |
}; |
| 112 |
|
| 113 |
const init = (root) => { |
| 114 |
const ctx = root && root.querySelectorAll ? root : document; |
| 115 |
ctx.querySelectorAll('.ka-woo-cart-totals').forEach((wrap) => bind(wrap)); |
| 116 |
}; |
| 117 |
|
| 118 |
return { init }; |
| 119 |
})(); |
| 120 |
|
| 121 |
window.KACartTotals = KACartTotals; |
| 122 |
|
| 123 |
const initInScope = ($scope) => { |
| 124 |
const root = $scope && $scope[0] ? $scope[0] : document; |
| 125 |
KACartTotals.init(root); |
| 126 |
}; |
| 127 |
|
| 128 |
document.addEventListener("DOMContentLoaded", () => KACartTotals.init(document)); |
| 129 |
|
| 130 |
$(window).on("elementor/frontend/init", function () { |
| 131 |
elementorFrontend.hooks.addAction( |
| 132 |
"frontend/element_ready/woo_cart_totals.default", |
| 133 |
function ($scope) { |
| 134 |
initInScope($scope); |
| 135 |
} |
| 136 |
); |
| 137 |
}); |
| 138 |
})(jQuery); |
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|