PluginProbe
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder / 51.1.79
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder v51.1.79
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.79, at includes/widgets/Woo_Cart_Coupon_Form/script.js

163 lines 4.8 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 if (window.KACartTable && typeof window.KACartTable.init === "function") {
23 window.KACartTable.init(document);
24 }
25 });
26 }
27
28 if (data.totals_html !== undefined) {
29 document.querySelectorAll('.ka-woo-cart-totals').forEach((totals) => {
30 totals.innerHTML = data.totals_html || '';
31 if (window.KACartTotals && typeof window.KACartTotals.init === "function") {
32 window.KACartTotals.init(document);
33 }
34 });
35 }
36
37 if (data.cross_sells_html !== undefined) {
38 document.querySelectorAll('.ka-woo-cart-cross-sells').forEach((cross) => {
39 cross.innerHTML = data.cross_sells_html || '';
40 if (window.KACartCrossSells && typeof window.KACartCrossSells.init === "function") {
41 window.KACartCrossSells.init();
42 }
43 });
44 }
45
46 if (data.notices) {
47 let container = document.querySelector('.woocommerce-notices-wrapper');
48 if (!container) {
49 container = document.createElement('div');
50 container.className = 'woocommerce-notices-wrapper';
51 document.body.prepend(container);
52 }
53 container.innerHTML = data.notices;
54 }
55 };
56
57 const sendCoupon = (wrap, mode, code) => {
58 const ajaxUrl = wrap.dataset.ajaxUrl;
59 const nonce = wrap.dataset.nonce;
60 if (!ajaxUrl || !nonce || !mode) return;
61
62 const applyBtn = wrap.querySelector('.ka-woo-cart-coupon__apply');
63 const removeBtn = wrap.querySelector('.ka-woo-cart-coupon__remove');
64 [applyBtn, removeBtn].forEach((btn) => {
65 if (btn) {
66 btn.disabled = true;
67 btn.classList.add('is-loading');
68 }
69 });
70
71 const body = new URLSearchParams();
72 body.append('action', 'ka_cart_coupon');
73 body.append('nonce', nonce);
74 body.append('mode', mode);
75 body.append('coupon', code || '');
76
77 fetch(ajaxUrl, {
78 method: 'POST',
79 headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
80 body: body.toString(),
81 credentials: 'same-origin',
82 })
83 .then((res) => res.json())
84 .then((res) => {
85 if (res && res.success && res.data) {
86 updateFragments(res.data);
87 setNotice(wrap, res.data.notices || '');
88 } else {
89 setNotice(wrap, '');
90 }
91 })
92 .catch(() => setNotice(wrap, ''))
93 .finally(() => {
94 [applyBtn, removeBtn].forEach((btn) => {
95 if (btn) {
96 btn.disabled = false;
97 btn.classList.remove('is-loading');
98 }
99 });
100 });
101 };
102
103 const bindWidget = (wrap) => {
104 if (!wrap || !wrap.dataset) return;
105 if (wrap.dataset[INIT_FLAG] === "1") return;
106 wrap.dataset[INIT_FLAG] = "1";
107
108 const toggle = wrap.querySelector('[data-ka-coupon-toggle]');
109 const body = wrap.querySelector('.ka-woo-cart-coupon__body');
110 const input = wrap.querySelector('.ka-woo-cart-coupon__input');
111 const applyBtn = wrap.querySelector('.ka-woo-cart-coupon__apply');
112 const removeBtn = wrap.querySelector('.ka-woo-cart-coupon__remove');
113
114 if (toggle && body) {
115 toggle.addEventListener('click', () => {
116 const isHidden = body.hasAttribute('hidden');
117 if (isHidden) {
118 body.removeAttribute('hidden');
119 } else {
120 body.setAttribute('hidden', '');
121 }
122 });
123 }
124
125 if (applyBtn && input) {
126 applyBtn.addEventListener('click', () => sendCoupon(wrap, 'apply', input.value));
127 }
128
129 if (removeBtn && input) {
130 removeBtn.addEventListener('click', () => sendCoupon(wrap, 'remove', input.value));
131 }
132
133 wrap.addEventListener('keydown', (event) => {
134 if (event.key === 'Enter' && input && document.activeElement === input) {
135 event.preventDefault();
136 sendCoupon(wrap, 'apply', input.value);
137 }
138 });
139 };
140
141 const init = (root) => {
142 const ctx = root && root.querySelectorAll ? root : document;
143 ctx.querySelectorAll('.ka-woo-cart-coupon-widget').forEach(bindWidget);
144 };
145
146 document.addEventListener("DOMContentLoaded", () => init(document));
147
148 $(window).on("elementor/frontend/init", function () {
149 elementorFrontend.hooks.addAction(
150 "frontend/element_ready/woo_cart_coupon_form.default",
151 function ($scope) {
152 init($scope && $scope[0] ? $scope[0] : document);
153 }
154 );
155 });
156 })(jQuery);
157
158
159
160
161
162
163