PluginProbe
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder / 51.1.86
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder v51.1.86
51.1.86 51.1.84 51.1.85 51.1.83 51.1.82 51.1.81 51.1.79 51.1.78 51.1.77 51.1.76 51.1.74 51.1.75 51.1.65 51.1.64 51.1.63 trunk 51.1.14 51.1.2 51.1.35 51.1.36 51.1.37 51.1.38 51.1.39 51.1.44 51.1.45 All 40 releases
king-addons / includes / widgets / Woo_Cart_Coupon_Form / script.js

script.js in King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder 51.1.86, at includes/widgets/Woo_Cart_Coupon_Form/script.js

175 lines 5.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 let container = document.querySelector('.woocommerce-notices-wrapper');
58 if (!container) {
59 container = document.createElement('div');
60 container.className = 'woocommerce-notices-wrapper';
61 document.body.prepend(container);
62 }
63 container.innerHTML = data.notices;
64 }
65
66 document.body.dispatchEvent(new Event('wc_fragments_refreshed'));
67 };
68
69 const sendCoupon = (wrap, mode, code) => {
70 const ajaxUrl = wrap.dataset.ajaxUrl;
71 const nonce = wrap.dataset.nonce;
72 if (!ajaxUrl || !nonce || !mode) return;
73
74 const applyBtn = wrap.querySelector('.ka-woo-cart-coupon__apply');
75 const removeBtn = wrap.querySelector('.ka-woo-cart-coupon__remove');
76 [applyBtn, removeBtn].forEach((btn) => {
77 if (btn) {
78 btn.disabled = true;
79 btn.classList.add('is-loading');
80 }
81 });
82
83 const body = new URLSearchParams();
84 body.append('action', 'ka_cart_coupon');
85 body.append('nonce', nonce);
86 body.append('mode', mode);
87 body.append('coupon', code || '');
88
89 fetch(ajaxUrl, {
90 method: 'POST',
91 headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
92 body: body.toString(),
93 credentials: 'same-origin',
94 })
95 .then((res) => res.json())
96 .then((res) => {
97 if (res && res.success && res.data) {
98 updateFragments(res.data);
99 setNotice(wrap, res.data.notices || '');
100 } else {
101 setNotice(wrap, '');
102 }
103 })
104 .catch(() => setNotice(wrap, ''))
105 .finally(() => {
106 [applyBtn, removeBtn].forEach((btn) => {
107 if (btn) {
108 btn.disabled = false;
109 btn.classList.remove('is-loading');
110 }
111 });
112 });
113 };
114
115 const bindWidget = (wrap) => {
116 if (!wrap || !wrap.dataset) return;
117 if (wrap.dataset[INIT_FLAG] === "1") return;
118 wrap.dataset[INIT_FLAG] = "1";
119
120 const toggle = wrap.querySelector('[data-ka-coupon-toggle]');
121 const body = wrap.querySelector('.ka-woo-cart-coupon__body');
122 const input = wrap.querySelector('.ka-woo-cart-coupon__input');
123 const applyBtn = wrap.querySelector('.ka-woo-cart-coupon__apply');
124 const removeBtn = wrap.querySelector('.ka-woo-cart-coupon__remove');
125
126 if (toggle && body) {
127 toggle.addEventListener('click', () => {
128 const isHidden = body.hasAttribute('hidden');
129 if (isHidden) {
130 body.removeAttribute('hidden');
131 } else {
132 body.setAttribute('hidden', '');
133 }
134 });
135 }
136
137 if (applyBtn && input) {
138 applyBtn.addEventListener('click', () => sendCoupon(wrap, 'apply', input.value));
139 }
140
141 if (removeBtn && input) {
142 removeBtn.addEventListener('click', () => sendCoupon(wrap, 'remove', input.value));
143 }
144
145 wrap.addEventListener('keydown', (event) => {
146 if (event.key === 'Enter' && input && document.activeElement === input) {
147 event.preventDefault();
148 sendCoupon(wrap, 'apply', input.value);
149 }
150 });
151 };
152
153 const init = (root) => {
154 const ctx = root && root.querySelectorAll ? root : document;
155 ctx.querySelectorAll('.ka-woo-cart-coupon-widget').forEach(bindWidget);
156 };
157
158 document.addEventListener("DOMContentLoaded", () => init(document));
159
160 $(window).on("elementor/frontend/init", function () {
161 elementorFrontend.hooks.addAction(
162 "frontend/element_ready/woo_cart_coupon_form.default",
163 function ($scope) {
164 init($scope && $scope[0] ? $scope[0] : document);
165 }
166 );
167 });
168 })(jQuery);
169
170
171
172
173
174
175