| 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 |
|