| 1 |
/** |
| 2 |
* Woo Cart Coupon Form widget behavior. |
| 3 |
* |
| 4 |
* Applies/removes coupons via AJAX and refreshes cart fragments. |
| 5 |
*/ |
| 6 |
(function ($) { |
| 7 |
"use strict"; |
| 8 |
|
| 9 |
const INIT_FLAG = "kaWooCartCouponInit"; |
| 10 |
|
| 11 |
const setNotice = (wrap, html) => { |
| 12 |
const notice = wrap.querySelector('.ka-woo-cart-coupon__notice'); |
| 13 |
if (notice) { |
| 14 |
notice.innerHTML = html || ''; |
| 15 |
} |
| 16 |
}; |
| 17 |
|
| 18 |
const updateFragments = (data) => { |
| 19 |
if (data.cart_html && document.querySelector('.ka-cart-table')) { |
| 20 |
document.querySelectorAll('.ka-cart-table').forEach((table) => { |
| 21 |
table.innerHTML = data.cart_html; |
| 22 |
// Those widgets mark a wrapper as bound and skip it next time. The |
| 23 |
// listeners lived on the markup just replaced, so without clearing the |
| 24 |
// flag the cart's quantity and remove controls went dead after |
| 25 |
// applying a coupon from here. |
| 26 |
if (table.dataset) { |
| 27 |
delete table.dataset.kaCartTableInit; |
| 28 |
} |
| 29 |
if (window.KACartTable && typeof window.KACartTable.init === "function") { |
| 30 |
window.KACartTable.init(document); |
| 31 |
} |
| 32 |
}); |
| 33 |
} |
| 34 |
|
| 35 |
if (data.totals_html !== undefined) { |
| 36 |
document.querySelectorAll('.ka-woo-cart-totals').forEach((totals) => { |
| 37 |
totals.innerHTML = data.totals_html || ''; |
| 38 |
if (totals.dataset) { |
| 39 |
delete totals.dataset.kaCartTotalsInit; |
| 40 |
} |
| 41 |
if (window.KACartTotals && typeof window.KACartTotals.init === "function") { |
| 42 |
window.KACartTotals.init(document); |
| 43 |
} |
| 44 |
}); |
| 45 |
} |
| 46 |
|
| 47 |
if (data.cross_sells_html !== undefined) { |
| 48 |
document.querySelectorAll('.ka-woo-cart-cross-sells').forEach((cross) => { |
| 49 |
cross.innerHTML = data.cross_sells_html || ''; |
| 50 |
if (window.KACartCrossSells && typeof window.KACartCrossSells.init === "function") { |
| 51 |
window.KACartCrossSells.init(); |
| 52 |
} |
| 53 |
}); |
| 54 |
} |
| 55 |
|
| 56 |
if (data.notices) { |
| 57 |
const container = document.querySelector('.woocommerce-notices-wrapper'); |
| 58 |
if (container) { |
| 59 |
container.innerHTML = data.notices; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
document.body.dispatchEvent(new Event('wc_fragments_refreshed')); |
| 64 |
}; |
| 65 |
|
| 66 |
const sendCoupon = (wrap, mode, code) => { |
| 67 |
const ajaxUrl = wrap.dataset.ajaxUrl; |
| 68 |
const nonce = wrap.dataset.nonce; |
| 69 |
if (!ajaxUrl || !nonce || !mode) return; |
| 70 |
|
| 71 |
const applyBtn = wrap.querySelector('.ka-woo-cart-coupon__apply'); |
| 72 |
const removeBtn = wrap.querySelector('.ka-woo-cart-coupon__remove'); |
| 73 |
[applyBtn, removeBtn].forEach((btn) => { |
| 74 |
if (btn) { |
| 75 |
btn.disabled = true; |
| 76 |
btn.classList.add('is-loading'); |
| 77 |
} |
| 78 |
}); |
| 79 |
|
| 80 |
const body = new URLSearchParams(); |
| 81 |
body.append('action', 'ka_cart_coupon'); |
| 82 |
body.append('nonce', nonce); |
| 83 |
body.append('mode', mode); |
| 84 |
body.append('coupon', code || ''); |
| 85 |
|
| 86 |
fetch(ajaxUrl, { |
| 87 |
method: 'POST', |
| 88 |
headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' }, |
| 89 |
body: body.toString(), |
| 90 |
credentials: 'same-origin', |
| 91 |
}) |
| 92 |
.then((res) => res.json()) |
| 93 |
.then((res) => { |
| 94 |
const data = res && res.data ? res.data : {}; |
| 95 |
if (data.cart_html || data.totals_html !== undefined || data.notices) { |
| 96 |
updateFragments(data); |
| 97 |
} |
| 98 |
let html = data.notices || ''; |
| 99 |
if (!html && data.message) { |
| 100 |
const safe = String(data.message).replace(/[&<>"']/g, (ch) => ({ |
| 101 |
'&': '&', |
| 102 |
'<': '<', |
| 103 |
'>': '>', |
| 104 |
'"': '"', |
| 105 |
"'": ''', |
| 106 |
}[ch])); |
| 107 |
html = '<div class="woocommerce-error" role="alert">' + safe + '</div>'; |
| 108 |
} |
| 109 |
setNotice(wrap, html); |
| 110 |
const noticeEl = wrap.querySelector('.ka-woo-cart-coupon__notice'); |
| 111 |
if (html && noticeEl && typeof noticeEl.scrollIntoView === 'function') { |
| 112 |
noticeEl.scrollIntoView({ block: 'nearest' }); |
| 113 |
} |
| 114 |
}) |
| 115 |
.catch(() => setNotice(wrap, '<div class="woocommerce-error" role="alert">Could not apply coupon.</div>')) |
| 116 |
.finally(() => { |
| 117 |
[applyBtn, removeBtn].forEach((btn) => { |
| 118 |
if (btn) { |
| 119 |
btn.disabled = false; |
| 120 |
btn.classList.remove('is-loading'); |
| 121 |
} |
| 122 |
}); |
| 123 |
}); |
| 124 |
}; |
| 125 |
|
| 126 |
const bindWidget = (wrap) => { |
| 127 |
if (!wrap || !wrap.dataset) return; |
| 128 |
if (wrap.dataset[INIT_FLAG] === "1") return; |
| 129 |
wrap.dataset[INIT_FLAG] = "1"; |
| 130 |
|
| 131 |
const toggle = wrap.querySelector('[data-ka-coupon-toggle]'); |
| 132 |
const body = wrap.querySelector('.ka-woo-cart-coupon__body'); |
| 133 |
const input = wrap.querySelector('.ka-woo-cart-coupon__input'); |
| 134 |
const applyBtn = wrap.querySelector('.ka-woo-cart-coupon__apply'); |
| 135 |
const removeBtn = wrap.querySelector('.ka-woo-cart-coupon__remove'); |
| 136 |
|
| 137 |
if (toggle && body) { |
| 138 |
toggle.addEventListener('click', () => { |
| 139 |
const isHidden = body.hasAttribute('hidden'); |
| 140 |
if (isHidden) { |
| 141 |
body.removeAttribute('hidden'); |
| 142 |
} else { |
| 143 |
body.setAttribute('hidden', ''); |
| 144 |
} |
| 145 |
}); |
| 146 |
} |
| 147 |
|
| 148 |
if (applyBtn && input) { |
| 149 |
applyBtn.addEventListener('click', () => sendCoupon(wrap, 'apply', input.value)); |
| 150 |
} |
| 151 |
|
| 152 |
if (removeBtn && input) { |
| 153 |
removeBtn.addEventListener('click', () => sendCoupon(wrap, 'remove', input.value)); |
| 154 |
} |
| 155 |
|
| 156 |
wrap.addEventListener('keydown', (event) => { |
| 157 |
if (event.key === 'Enter' && input && document.activeElement === input) { |
| 158 |
event.preventDefault(); |
| 159 |
sendCoupon(wrap, 'apply', input.value); |
| 160 |
} |
| 161 |
}); |
| 162 |
}; |
| 163 |
|
| 164 |
const init = (root) => { |
| 165 |
const ctx = root && root.querySelectorAll ? root : document; |
| 166 |
ctx.querySelectorAll('.ka-woo-cart-coupon-widget').forEach(bindWidget); |
| 167 |
}; |
| 168 |
|
| 169 |
document.addEventListener("DOMContentLoaded", () => init(document)); |
| 170 |
|
| 171 |
$(window).on("elementor/frontend/init", function () { |
| 172 |
elementorFrontend.hooks.addAction( |
| 173 |
"frontend/element_ready/woo_cart_coupon_form.default", |
| 174 |
function ($scope) { |
| 175 |
init($scope && $scope[0] ? $scope[0] : document); |
| 176 |
} |
| 177 |
); |
| 178 |
}); |
| 179 |
})(jQuery); |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|