PluginProbe
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder / 51.1.83
King Addons for Elementor – 80+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce, Mega Menu, Popup Builder v51.1.83
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 51.1.46 51.1.47 51.1.49 All 37 releases
king-addons / includes / widgets / Ajax_Add_To_Cart / script.js

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

121 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 "use strict";
2
3 (function ($) {
4 const ajaxUrl = (endpoint) => {
5 if (typeof wc_add_to_cart_params !== "undefined") {
6 return wc_add_to_cart_params.wc_ajax_url.replace("%%endpoint%%", endpoint);
7 }
8 return "";
9 };
10
11 const sendAddToCart = (productId, quantity) => {
12 const url = ajaxUrl("add_to_cart");
13 if (!url) {
14 return Promise.reject(new Error("Missing WooCommerce AJAX URL"));
15 }
16 return $.post(url, {
17 product_id: productId,
18 quantity: quantity,
19 });
20 };
21
22 const initAjaxAtc = ($scope) => {
23 const wrapper = $scope[0]?.querySelector(".king-addons-ajax-atc");
24 if (!wrapper) {
25 return;
26 }
27
28 const button = wrapper.querySelector(".king-addons-ajax-atc__button");
29 const notice = wrapper.querySelector(".king-addons-ajax-atc__notice");
30 if (!button) {
31 return;
32 }
33
34 const productId = parseInt(wrapper.dataset.productId || "0", 10);
35 const successText = wrapper.dataset.successText || "Added to cart";
36 const redirectCheckout = wrapper.dataset.redirectCheckout === "yes";
37 const refreshFragments = wrapper.dataset.refreshFragments !== "no";
38 const quantityField = wrapper.querySelector(".king-addons-ajax-atc__quantity");
39
40 const setLoading = (isLoading) => {
41 button.classList.toggle("loading", isLoading);
42 };
43
44 const setSuccess = (isSuccess) => {
45 button.classList.toggle("added", isSuccess);
46 };
47
48 const setNotice = (message, isError = false) => {
49 if (!notice) {
50 return;
51 }
52 notice.textContent = message || "";
53 notice.style.color = isError ? "#dc2626" : "";
54 };
55
56 button.addEventListener("click", (event) => {
57 event.preventDefault();
58 if (!productId) {
59 setNotice("Product not found", true);
60 return;
61 }
62
63 // Reset success state on each new attempt (Woo behavior).
64 setSuccess(false);
65
66 setNotice("");
67 setLoading(true);
68
69 const qty = quantityField
70 ? Math.max(1, parseInt(quantityField.value || "1", 10))
71 : parseInt(wrapper.dataset.quantity || "1", 10) || 1;
72
73 sendAddToCart(productId, qty)
74 .done((response) => {
75 setLoading(false);
76 if (response && response.error) {
77 setNotice(response.product_url ? "" : (response.message || "Error"), true);
78 if (response.product_url) {
79 window.location.href = response.product_url;
80 }
81 return;
82 }
83
84 // No text on success: show checkmark on the button instead.
85 setNotice("", false);
86 setSuccess(true);
87
88 if (refreshFragments && response && response.fragments) {
89 $.each(response.fragments, function (key, value) {
90 $(key).replaceWith(value);
91 });
92 }
93
94 if (redirectCheckout && response && response.cart_hash) {
95 window.location.href = wc_add_to_cart_params?.cart_url || "/";
96 }
97 })
98 .fail(() => {
99 setLoading(false);
100 setNotice("Error adding to cart", true);
101 });
102 });
103 };
104
105 $(window).on("elementor/frontend/init", () => {
106 elementorFrontend.hooks.addAction(
107 "frontend/element_ready/king-addons-ajax-add-to-cart.default",
108 ($scope) => {
109 initAjaxAtc($scope);
110 }
111 );
112 });
113 })(jQuery);
114
115
116
117
118
119
120
121