PluginProbe
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder / 51.1.63
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder v51.1.63
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_Totals / script.js

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

135 lines 3.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 });
36 }
37 if (data.cart_html && document.querySelector('.ka-cart-table')) {
38 document.querySelector('.ka-cart-table').innerHTML = data.cart_html;
39 if (window.KACartTable && typeof window.KACartTable.init === "function") {
40 window.KACartTable.init(document);
41 }
42 }
43 if (data.cross_sells_html !== undefined) {
44 document.querySelectorAll('.ka-woo-cart-cross-sells').forEach((cross) => {
45 cross.innerHTML = data.cross_sells_html || '';
46 });
47 if (window.KACartCrossSells && typeof window.KACartCrossSells.init === "function") {
48 window.KACartCrossSells.init();
49 }
50 }
51 };
52
53 const sendCoupon = (wrap, mode, code) => {
54 const ajaxUrl = wrap.dataset.ajaxUrl;
55 const nonce = wrap.dataset.nonce;
56 if (!ajaxUrl || !nonce) return;
57
58 const body = new URLSearchParams();
59 body.append('action', 'ka_cart_coupon');
60 body.append('nonce', nonce);
61 body.append('mode', mode);
62 body.append('coupon', code);
63
64 wrap.classList.add('loading');
65
66 fetch(ajaxUrl, {
67 method: 'POST',
68 headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8' },
69 body: body.toString(),
70 credentials: 'same-origin',
71 })
72 .then((res) => res.json())
73 .then((res) => {
74 if (res && res.success) {
75 applyFragments(res.data || {});
76 }
77 setNotice(wrap, (res && res.data && res.data.notices) || '');
78 })
79 .catch(() => setNotice(wrap, ''))
80 .finally(() => wrap.classList.remove('loading'));
81 };
82
83 const bind = (wrap) => {
84 if (!wrap || !wrap.dataset) return;
85 if (wrap.dataset[FLAG] === "1") return;
86 wrap.dataset[FLAG] = "1";
87
88 const toggle = wrap.querySelector('[data-ka-coupon-toggle]');
89 const input = wrap.querySelector('.ka-cart-coupon__input');
90 const applyBtn = wrap.querySelector('.ka-cart-coupon__apply');
91 const removeBtn = wrap.querySelector('.ka-cart-coupon__remove');
92
93 if (toggle) {
94 toggle.addEventListener('click', () => toggleBody(wrap));
95 }
96 if (applyBtn && input) {
97 applyBtn.addEventListener('click', () => sendCoupon(wrap, 'apply', input.value));
98 }
99 if (removeBtn && input) {
100 removeBtn.addEventListener('click', () => sendCoupon(wrap, 'remove', input.value));
101 }
102 };
103
104 const init = (root) => {
105 const ctx = root && root.querySelectorAll ? root : document;
106 ctx.querySelectorAll('.ka-woo-cart-totals').forEach((wrap) => bind(wrap));
107 };
108
109 return { init };
110 })();
111
112 window.KACartTotals = KACartTotals;
113
114 const initInScope = ($scope) => {
115 const root = $scope && $scope[0] ? $scope[0] : document;
116 KACartTotals.init(root);
117 };
118
119 document.addEventListener("DOMContentLoaded", () => KACartTotals.init(document));
120
121 $(window).on("elementor/frontend/init", function () {
122 elementorFrontend.hooks.addAction(
123 "frontend/element_ready/woo_cart_totals.default",
124 function ($scope) {
125 initInScope($scope);
126 }
127 );
128 });
129 })(jQuery);
130
131
132
133
134
135