PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 3.13.3
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v3.13.3
3.13.3 3.13.2 3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 2.8.16 All 261 releases
wpfunnels / public / assets / js / wpfnl-offer.js

wpfnl-offer.js in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 3.13.3, at public/assets/js/wpfnl-offer.js

957 lines 38.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 (function ($) {
2 "use strict";
3
4 /**
5 * All of the code for your public-facing JavaScript source
6 * should reside in this file.
7 *
8 * Note: It has been assumed you will write jQuery code here, so the
9 * $ function reference has been prepared for usage within the scope
10 * of this function.
11 *
12 * This enables you to define handlers, for when the DOM is ready:
13 *
14 * $(function() {
15 *
16 * });
17 *
18 * When the window is loaded:
19 *
20 * $( window ).load(function() {
21 *
22 * });
23 *
24 * ...and/or other possibilities.
25 *
26 * Ideally, it is not considered best practise to attach more than a
27 * single DOM-ready or window-load handler for a particular page.
28 * Although scripts in the WordPress core, Plugins and Themes may be
29 * practising this, we should strive to set a better example in our own work.
30 */
31
32 /**
33 * process offer order
34 *
35 * @param data
36 */
37 var wpfunnels_process_offer = function (data) {
38 var ajaxurl = wpfnl_obj.ajaxurl;
39 $.ajax({
40 type: "POST",
41 url: ajaxurl,
42 data: data,
43 success: function (response) {
44 $(".wpfunnels-offer-loader p.description").text(response.message);
45 if (response.status === "success") {
46 $("#wpfnl-alert-accept").addClass("wpfnl-success");
47 if (response.redirect_url != undefined) {
48 setTimeout(function () {
49 $(".wpfunnels-offer-loader p.description").text("Redirecting...");
50 window.location.href = response.redirect_url;
51 }, 1000);
52 }
53 } else {
54 $(".wpfunnels-offer-loader p.description").text(response.message);
55 setTimeout(function () {
56 $(".wpfunnels-offer-loader").hide();
57 }, 1500);
58 }
59 },
60 error: function (xhr) {
61 // A rejected request (e.g. an expired nonce, or an order this
62 // visitor does not own) comes back as an HTTP error, so the
63 // success handler never runs. Without this the offer overlay
64 // would spin forever.
65 var message = "Something went wrong. Please refresh the page and try again.";
66 if (xhr && xhr.responseJSON && xhr.responseJSON.data && xhr.responseJSON.data.message) {
67 message = xhr.responseJSON.data.message;
68 }
69 $(".wpfunnels-offer-loader p.description").text(message);
70 setTimeout(function () {
71 $(".wpfunnels-offer-loader").hide();
72 }, 1500);
73 },
74 });
75 };
76
77 /**
78 * get query param from url
79 *
80 * @param sParam
81 * @returns {boolean|string}
82 */
83 function getUrlParameter(sParam) {
84 var sPageURL = window.location.search.substring(1),
85 sURLVariables = sPageURL.split("&"),
86 sParameterName,
87 i;
88 for (i = 0; i < sURLVariables.length; i++) {
89 sParameterName = sURLVariables[i].split("=");
90
91 if (sParameterName[0] === sParam) {
92 return sParameterName[1] === undefined ? true : decodeURIComponent(sParameterName[1]);
93 }
94 }
95 }
96
97 function wpfunnels_capture_woocommerce_paypal_order() {
98 if ("undefined" !== typeof window.WPFunnelsOfferVars) {
99 if ("upsell" === window.WPFunnelsOfferVars.offer_type || "downsell" === window.WPFunnelsOfferVars.offer_type) {
100 var is_paypal_return = getUrlParameter("wpfnl-paypal-return"),
101 security = "";
102
103 if (is_paypal_return) {
104 security = window.WPFunnelsOfferVars.wpfnl_capture_paypal_order_nonce;
105 var ajax_data = {
106 order_id: window.WPFunnelsOfferVars.order_id,
107 order_key: window.WPFunnelsOfferVars.order_key,
108 step_id: window.WPFunnelsOfferVars.step_id,
109 action: "wpfnl_capture_paypal_order",
110 step_type: window.WPFunnelsOfferVars.offer_type,
111 security: security,
112 };
113 $(".wpfunnels-offer-loader").show();
114 $.ajax({
115 url: window.WPFunnelsOfferVars.ajaxUrl,
116 data: ajax_data,
117 dataType: "json",
118 type: "POST",
119 error: function (xhr) {
120 var message = "Something went wrong. Please refresh the page and try again.";
121 if (xhr && xhr.responseJSON && xhr.responseJSON.data && xhr.responseJSON.data.message) {
122 message = xhr.responseJSON.data.message;
123 }
124 $(".wpfunnels-offer-loader p.description").text(message);
125 setTimeout(function () {
126 $(".wpfunnels-offer-loader").hide();
127 }, 1500);
128 },
129 success: function (response) {
130 var ajax_data = {
131 action: "",
132 step_id: window.WPFunnelsOfferVars.step_id,
133 funnel_id: window.WPFunnelsOfferVars.funnel_id,
134 order_id: window.WPFunnelsOfferVars.order_id,
135 order_key: window.WPFunnelsOfferVars.order_key,
136 offer_type: window.WPFunnelsOfferVars.offer_type,
137 offer_action: "yes",
138 product_id: window.WPFunnelsOfferVars.product_id,
139 quantity: window.WPFunnelsOfferVars.quantity,
140 attr: "",
141 is_variable: window.WPFunnelsOfferVars.is_variable,
142 };
143 if (window.WPFunnelsOfferVars.is_variable) {
144 ajax_data.variation_id = getUrlParameter("wpfnl-variation-id");
145 }
146
147 var offer_type = window.WPFunnelsOfferVars.offer_type;
148 if ("upsell" === ajax_data.offer_type) {
149 ajax_data.action = "wpfnl_" + offer_type + "_accepted";
150 ajax_data.security = window.WPFunnelsOfferVars.wpfnl_upsell_accepted_nonce;
151 } else if ("downsell" === ajax_data.offer_type) {
152 ajax_data.action = "wpfnl_" + offer_type + "_accepted";
153 ajax_data.security = window.WPFunnelsOfferVars.wpfnl_downsell_accepted_nonce;
154 }
155
156 wpfunnels_process_offer(ajax_data);
157 },
158 });
159 }
160 }
161 }
162 }
163
164 /**
165 * capture mollie payment from url
166 */
167 var wpfunnels_capture_woocommerce_mollie_order = function () {
168 let is_mollie_return = getUrlParameter("wpfnl-mollie-return"),
169 nonce = "";
170
171 if (is_mollie_return) {
172 var offer_type = window.WPFunnelsOfferVars.offer_type;
173 var ajax_data = {
174 action: "",
175 step_id: window.WPFunnelsOfferVars.step_id,
176 funnel_id: window.WPFunnelsOfferVars.funnel_id,
177 order_id: window.WPFunnelsOfferVars.order_id,
178 order_key: window.WPFunnelsOfferVars.order_key,
179 offer_type: window.WPFunnelsOfferVars.offer_type,
180 offer_action: "yes",
181 product_id: window.WPFunnelsOfferVars.product_id,
182 quantity: window.WPFunnelsOfferVars.quantity,
183 security: nonce,
184 };
185 if ("upsell" === offer_type) {
186 ajax_data.action = "wpfnl_" + offer_type + "_accepted";
187 ajax_data.security = window.WPFunnelsOfferVars.wpfnl_upsell_accepted_nonce;
188 } else if ("downsell" === offer_type) {
189 ajax_data.action = "wpfnl_" + offer_type + "_accepted";
190 ajax_data.security = window.WPFunnelsOfferVars.wpfnl_downsell_accepted_nonce;
191 }
192 $(".wpfunnels-offer-loader").show();
193 wpfunnels_process_offer(ajax_data);
194 }
195 };
196
197 /**
198 * process mollie payment with
199 * ajax
200 *
201 * @param ajax_data
202 * @param gateway
203 */
204 function wpfunnels_process_mollie_payments(ajax_data, gateway) {
205 if ("mollie_wc_gateway_creditcard" === gateway) {
206 ajax_data.action = "wpf_mollie_credit_card_payment_process";
207 ajax_data.security = window.WPFunnelsOfferVars.wpfnl_mollie_cc_process_nonce;
208 } else if ("mollie_wc_gateway_creditcard" === gateway) {
209 ajax_data.action = "wpf_mollie_ideal_payment_process";
210 ajax_data.security = window.WPFunnelsOfferVars.wpfnl_mollie_ideal_process_nonce;
211 }
212
213 $.ajax({
214 url: window.WPFunnelsOfferVars.ajaxUrl,
215 data: ajax_data,
216 dataType: "json",
217 type: "POST",
218 success: function (response) {
219 if ("success" === response.result) {
220 window.location.href = response.redirect;
221 }
222 },
223 });
224 }
225
226 /**
227 * create paypal order for WC paypal
228 * @param ajax_data
229 */
230 function wpfunnels_create_paypal_order(ajax_data) {
231 $.ajax({
232 url: window.WPFunnelsOfferVars.ajaxUrl,
233 data: ajax_data,
234 dataType: "json",
235 type: "POST",
236 success: function (response) {
237 if ("success" === response.status) {
238 window.location.href = response.redirect;
239 } else {
240 ajax_data.action = "wpfnl_" + ajax_data.offer_type + "_accepted";
241 if ("upsell" === ajax_data.offer_type) {
242 ajax_data.security = window.WPFunnelsOfferVars.wpfnl_upsell_accepted_nonce;
243 } else if ("downsell" === ajax_data.offer_type) {
244 ajax_data.security = window.WPFunnelsOfferVars.wpfnl_downsell_accepted_nonce;
245 }
246 wpfunnels_process_offer(ajax_data);
247 }
248 },
249 });
250 }
251
252 /**
253 * Get selected variation
254 *
255 * @returns Object
256 */
257 function getSelectedVariationID() {
258 let attr = {},
259 is_empty = false;
260
261 $(".wpfnl-variable-attribute-offer").each(function (index) {
262 if ($(this).val()) {
263 attr["attribute_" + $(this).attr("id")] = $(this).val();
264 } else {
265 is_empty = true;
266 }
267 });
268 let response = {
269 is_empty: is_empty,
270 attr: attr,
271 };
272 return response;
273 }
274
275 $(document).on("change", ".wpfnl-variable-attribute-offer", function () {
276 let response = getSelectedVariationID();
277 if (!response.is_empty) {
278 let payload = {
279 product_id: window.WPFunnelsOfferVars.product_id,
280 attr: response.attr,
281 };
282
283 $(".offer-btn-loader").show();
284
285 wpAjaxHelperRequest("wpfnl-get-variation-price", payload)
286 .success(function (response) {
287 if (response.success) {
288 $("#wpfnl-offer-product-price").empty();
289 $("#wpfnl-offer-product-price").html(response.data);
290 $(".offer-btn-loader").hide();
291 }
292 })
293 .error(function (response) {});
294 } else {
295 $("#wpfnl-offer-product-price").empty();
296 }
297 });
298
299 /**
300 * offer action ajax
301 */
302 $(document).on("click", ".wpfunnels_offer_button, .wpfunnels-block-offer-button", function (e) {
303 e.preventDefault();
304 let supportedPaymentGateway = [
305 // 'stripe',
306 'paypal',
307 'ppcp-gateway',
308 'cod',
309 'mollie_wc_gateway_creditcard',
310 'mollie_wc_gateway_ideal',
311 'authorize_net_cim_credit_card',
312 'officeguy',
313 'payfast',
314 'square_credit_card',
315 'bacs',
316 'cheque',
317 'woocommerce_payments'
318 ];
319
320 let ajaxurl = window.WPFunnelsOfferVars.ajaxUrl,
321 step_id = window.WPFunnelsOfferVars.step_id,
322 funnel_id = window.WPFunnelsOfferVars.funnel_id,
323 is_lms = window.WPFunnelsOfferVars.is_lms,
324 order_id = window.WPFunnelsOfferVars.order_id,
325 order_key = window.WPFunnelsOfferVars.order_key,
326 product_id = window.WPFunnelsOfferVars.product_id,
327 quantity = window.WPFunnelsOfferVars.quantity,
328 payment_method = window.WPFunnelsOfferVars.payment_method,
329 skip_offer = window.WPFunnelsOfferVars.skip_offer,
330 id = $(this).attr("id"),
331 offer_action = "yes",
332 offer_type = "upsell",
333 action = "wpfnl_upsell_accept",
334 security = "",
335 products = $(this).attr("data-products"),
336 next_step = $(this).attr("data-next-step");
337 let response = getSelectedVariationID();
338
339 /**
340 * check if upsell accepted or rejected
341 */
342 if (id.indexOf("wpfunnels_upsell") !== -1) {
343 offer_type = "upsell";
344 if (id.indexOf("wpfunnels_upsell_accept") !== -1) {
345 offer_action = "yes";
346 security = window.WPFunnelsOfferVars.wpfnl_upsell_accepted_nonce;
347 } else {
348 offer_action = "no";
349 security = window.WPFunnelsOfferVars.wpfnl_upsell_rejected_nonce;
350 }
351 }
352
353 /**
354 * check if downsell accepted or rejected
355 */
356 if (id.indexOf("wpfunnels_downsell") !== -1) {
357 offer_type = "downsell";
358 if (id.indexOf("wpfunnels_downsell_accept") !== -1) {
359 offer_action = "yes";
360 security = window.WPFunnelsOfferVars.wpfnl_downsell_accepted_nonce;
361 } else {
362 offer_action = "no";
363 security = window.WPFunnelsOfferVars.wpfnl_downsell_rejected_nonce;
364 }
365 }
366 if( 'yes' === offer_action && 'yes' === window.WPFunnelsOfferVars.maybe_unsupported_payment ){
367 var ajax_data = {
368 action: 'wpfnl_load_payment',
369 security: window.WPFunnelsOfferVars.wpfnl_offer_nonce,
370 product_id: product_id,
371 step_id : window.WPFunnelsOfferVars.step_id,
372 quantity: quantity,
373 attr: undefined !== response.attr && response.attr ? response.attr : "",
374 is_variable: window.WPFunnelsOfferVars.is_variable,
375 };
376 $('.wpfnl-offer-payment-form').show();
377 $.ajax({
378 type: "POST",
379 url: ajaxurl,
380 data: ajax_data,
381 success: function (response) {
382 jQuery(document.body).trigger('update_checkout');
383
384 $('.wpfnl-offer-payment-form .modal-loader').hide();
385 $('.wpfnl-offer-payment-form form.woocommerce-checkout').show();
386 },
387 });
388 }else{
389 /**
390 * define action of ajax
391 */
392 if ("yes" === offer_action) {
393 action = "wpfnl_" + offer_type + "_accepted";
394 $(".wpfunnels-offer-loader p.description").text("Please wait while processing your order");
395 } else {
396 action = "wpfnl_" + offer_type + "_rejected";
397 $(".wpfunnels-offer-loader p.description").text("Please wait...");
398 }
399 if ("yes" === skip_offer && "yes" === offer_action) {
400 return false;
401 }
402
403 if (offer_action == "yes" && response.is_empty && window.WPFunnelsOfferVars.is_variable) {
404 $(".wpfnl-select-variation").text("Please select the variation");
405 return false;
406 }
407
408 $(".wpfunnels-offer-loader").show();
409
410 if (is_lms === "yes" || (undefined !== order_id && undefined !== order_key)) {
411 var ajax_data = {
412 action: action,
413 step_id: step_id,
414 funnel_id: funnel_id,
415 order_id: order_id,
416 order_key: order_key,
417 offer_type: offer_type,
418 offer_action: offer_action,
419 products: products,
420 product_id: product_id,
421 quantity: quantity,
422 next_step: next_step,
423 stripe_sca_payment: false,
424 stripe_intent_id: "",
425 woop_intent_id: "",
426 attr: "",
427 is_variable: window.WPFunnelsOfferVars.is_variable,
428 };
429 if ("yes" === offer_action) {
430 if (window.WPFunnelsOfferVars.is_variable) {
431 ajax_data.attr = response.attr;
432 }
433
434 if ("stripe" === window.WPFunnelsOfferVars.payment_gateway) {
435 ajax_data.security = window.WPFunnelsOfferVars.wpfnl_stripe_sca_check_nonce;
436 ajax_data.action = "wpfunnels_stripe_sca_check";
437 $.ajax({
438 type: "POST",
439 url: ajaxurl,
440 data: ajax_data,
441 success: function (response) {
442 if (response.hasOwnProperty("intent_secret")) {
443 var stripe = Stripe(response.stripe_pk);
444 stripe
445 .handleCardPayment(response.intent_secret)
446 .then(function (response) {
447 if (response.error) {
448 throw response.error;
449 }
450 if ("requires_capture" !== response.paymentIntent.status && "succeeded" !== response.paymentIntent.status) {
451 return;
452 }
453
454 ajax_data.action = action;
455 ajax_data.stripe_sca_payment = true;
456 ajax_data.security = security;
457 ajax_data.stripe_intent_id = response.paymentIntent.id;
458 wpfunnels_process_offer(ajax_data);
459 })
460 .catch(function (error) {
461 window.location.reload();
462 });
463 } else {
464 ajax_data.action = action;
465 ajax_data.security = security;
466
467 wpfunnels_process_offer(ajax_data);
468 }
469 },
470 });
471 } else if ("woocommerce_payments" === window.WPFunnelsOfferVars.payment_gateway) {
472 ajax_data.security = window.WPFunnelsOfferVars.wpfunnels_woop_create_payment_intent;
473 ajax_data.action = "wpfunnels_woop_create_payment_intent";
474
475 $.ajax({
476 url: ajaxurl,
477 data: ajax_data,
478 dataType: "json",
479 type: "POST",
480 success(response) {
481 if (response.hasOwnProperty("client_secret") && "" !== response.client_secret && "succeeded" !== response.status) {
482 const stripe = Stripe(response.client_public);
483
484 stripe
485 .confirmCardPayment(response.client_secret)
486 .then(function (resp) {
487 if (resp.error) {
488 throw resp.error;
489 }
490
491 if ("requires_capture" !== resp.paymentIntent.status && "succeeded" !== resp.paymentIntent.status) {
492 console.log("Order not complete. Received status: " + resp.paymentIntent.status);
493 return;
494 }
495
496 ajax_data.action = action;
497 ajax_data.woop_intent_id = resp.paymentIntent.id;
498 ajax_data.woop_payment_method = resp.paymentIntent.payment_method;
499 wpfunnels_process_offer(ajax_data);
500 })
501 .catch(function () {
502 window.location.reload();
503 });
504 } else {
505 ajax_data.action = action;
506 ajax_data.security = security;
507 ajax_data.woop_intent_id = response.client_intend;
508 wpfunnels_process_offer(ajax_data);
509 }
510 },
511 });
512 } else if ("mollie_wc_gateway_creditcard" === window.WPFunnelsOfferVars.payment_gateway) {
513 wpfunnels_process_mollie_payments(ajax_data, window.WPFunnelsOfferVars.payment_gateway);
514 } else if ("mollie_wc_gateway_ideal" === window.WPFunnelsOfferVars.payment_gateway) {
515 wpfunnels_process_mollie_payments(ajax_data, window.WPFunnelsOfferVars.payment_gateway);
516 } else if ("ppcp-gateway" === window.WPFunnelsOfferVars.payment_gateway) {
517 ajax_data.security = window.WPFunnelsOfferVars.wpfnl_create_paypal_order_nonce;
518 ajax_data.action = "wpfnl_create_paypal_order";
519 wpfunnels_create_paypal_order(ajax_data);
520 } else {
521 ajax_data.security = security;
522
523 wpfunnels_process_offer(ajax_data);
524 }
525 } else {
526 ajax_data.action = action;
527 ajax_data.security = security;
528 wpfunnels_process_offer(ajax_data);
529 }
530 } else {
531 $("#wpfnl-loader-accept").hide();
532 $("#wpfnl-alert-accept").addClass("wpfnl-error");
533 $("#wpfnl-alert-accept").text("No order found to process");
534 }
535 }
536 });
537
538 $(document).on("click", ".wpfnl-offer-payment-form .close-modal", function (e) {
539 $('.wpfnl-offer-payment-form').hide();
540 });
541
542
543 //=== Upsell Reject ajax called==//
544 $(document).on("click", "#wpfunnels_upsell_reject", function (e) {
545 e.preventDefault();
546 $("#wpfnl-loader-reject").css("display", "inline-block");
547 $("#wpfnl-alert-reject").show();
548 var ajaxurl = wpfnl_obj.ajaxurl;
549 var step_id = $(this).attr("data-id");
550 var order_id = getUrlParameter("order_id");
551 var order_key = getUrlParameter("order_key");
552 var next_step = $(this).attr("data-next-step");
553 if (order_id != undefined && order_key != undefined) {
554 jQuery.ajax({
555 type: "POST",
556 url: ajaxurl,
557 data: {
558 action: "wpfnl_upsell_reject_ajax",
559 step_id: step_id,
560 order_id: order_id,
561 next_step: next_step,
562 order_key: order_key,
563 },
564 success: function (response) {
565 $("#wpfnl-loader-reject").hide();
566 setTimeout(function () {
567 window.location.href = response.redirect_url;
568 }, 500);
569 },
570 });
571 } else {
572 $("#wpfnl-loader-reject").hide();
573 $("#wpfnl-alert-reject").addClass("wpfnl-error");
574 $("#wpfnl-alert-reject").text("No order found to process");
575 }
576 });
577
578 wpfunnels_capture_woocommerce_paypal_order();
579 wpfunnels_capture_woocommerce_mollie_order();
580
581 /**
582 * facebook pixel for order bump added to
583 * cart
584 */
585 var wpfunnels_facebook_pixel = function () {
586 jQuery(document).ajaxComplete(function (event, xhr, settings) {
587 if (!xhr.hasOwnProperty("responseJSON")) {
588 return;
589 }
590
591 let fragments = xhr.responseJSON.hasOwnProperty("fragments") ? xhr.responseJSON.fragments : null;
592 if (fragments && fragments.hasOwnProperty("product_added_to_cart")) {
593 fbq("track", "AddToCart", fragments.product_added_to_cart.product_added_to_cart);
594 }
595 });
596 };
597
598 //== Variable product and single product add in checkout page ==//
599 $(document).ready(function () {
600 $(document).on("change", ".wpfnl-product-variation .input-checkbox", function (e) {
601 e.preventDefault();
602 var ajaxurl = window.wpfnl_obj.ajaxurl;
603 var step_id = window.wpfnl_obj.step_id;
604 var product_id = $(this).parent().data("id");
605 // var quantity = $(this).parent().data('qty');
606 var quantity = $(this).parent().parent().parent().parent().parent().find(".set-quantity").val();
607 let checker = false;
608 if (quantity == undefined) {
609 var quantity = $(this).parent().data("qty");
610 }
611 if ($(this).prop("checked") == true) {
612 checker = true;
613 } else if ($(this).prop("checked") == false) {
614 checker = false;
615 }
616
617 jQuery.ajax({
618 type: "POST",
619 url: ajaxurl,
620 data: {
621 action: "wpfnl_variable_ajax",
622 checker: checker,
623 quantity: quantity,
624 step_id: step_id,
625 product_id: product_id,
626 type: "checkbox",
627 },
628 success: function (response) {
629 if (response.success) {
630 if (response.data.status == "success") {
631 $("body").trigger("update_checkout");
632 } else {
633 console.log(response);
634 }
635 } else {
636 console.log(response);
637 }
638 },
639 });
640 });
641
642 $(document).on("change keyup", ".product-quantity .set-quantity", function (e) {
643 e.preventDefault();
644 var ajaxurl = window.wpfnl_obj.ajaxurl;
645 var step_id = window.wpfnl_obj.step_id;
646 var product_id = $(this).attr("product-id");
647 var quantity = $(this).val();
648 if (quantity < 1) {
649 return false;
650 }
651 let checker = false;
652 let isQuantity = true;
653 if ($(this).parent().parent().find(".input-checkbox").prop("checked") == undefined) {
654 if ($(this).parent().parent().find(".input-radio").prop("checked") == true) {
655 checker = true;
656 } else if ($(this).parent().parent().find(".input-radio").prop("checked") == false) {
657 checker = false;
658 return false;
659 }
660 }
661 if ($(this).parent().parent().find(".input-radio").prop("checked") == undefined) {
662 if ($(this).parent().parent().find(".input-checkbox").prop("checked") == true) {
663 checker = true;
664 } else if ($(this).parent().parent().find(".input-checkbox").prop("checked") == false) {
665 checker = false;
666 return false;
667 }
668 }
669 jQuery.ajax({
670 type: "POST",
671 url: ajaxurl,
672 data: {
673 action: "wpfnl_variable_ajax",
674 checker: checker,
675 quantity: quantity,
676 step_id: step_id,
677 product_id: product_id,
678 isQuantity: isQuantity,
679 type: "checkbox",
680 },
681 success: function (response) {
682 if (response.success) {
683 if (response.data.status == "success") {
684 $("body").trigger("update_checkout");
685 } else {
686 console.log(response);
687 }
688 } else {
689 console.log(response);
690 }
691 },
692 });
693 });
694
695 $(document).on("change keyup", ".single-product .product-quantity .set-quantity-single", function (e) {
696 e.preventDefault();
697 var ajaxurl = window.wpfnl_obj.ajaxurl;
698 var step_id = window.wpfnl_obj.step_id;
699 var product_id = $(this).attr("product-id");
700 var quantity = $(this).val();
701 if (quantity < 1) {
702 return false;
703 }
704 let isQuantity = true;
705 jQuery.ajax({
706 type: "POST",
707 url: ajaxurl,
708 data: {
709 action: "wpfnl_single_product_quantity_ajax",
710 quantity: quantity,
711 step_id: step_id,
712 product_id: product_id,
713 isQuantity: isQuantity,
714 },
715 success: function (response) {
716 if (response.success) {
717 if (response.data.status == "success") {
718 $("body").trigger("update_checkout");
719 } else {
720 console.log(response);
721 }
722 } else {
723 console.log(response);
724 }
725 },
726 });
727 });
728
729 $(document).on("change", ".wpfnl-product-variation .input-radio", function (e) {
730 e.preventDefault();
731 var ajaxurl = window.wpfnl_obj.ajaxurl;
732 var step_id = window.wpfnl_obj.step_id;
733 var product_id = $(this).data("id");
734 var quantity = $(this).parent().parent().parent().parent().find(".set-quantity").val();
735 if (quantity == undefined) {
736 var quantity = $(this).parent().data("qty");
737 }
738 let checker = true;
739 if (quantity < 1) {
740 return false;
741 }
742 if ($(this).prop("checked") == true) {
743 checker = true;
744 } else if ($(this).prop("checked") == false) {
745 checker = false;
746 }
747
748 jQuery.ajax({
749 type: "POST",
750 url: ajaxurl,
751 data: {
752 action: "wpfnl_variable_ajax",
753 checker: checker,
754 quantity: quantity,
755 step_id: step_id,
756 product_id: product_id,
757 type: "radio",
758 },
759 success: function (response) {
760 if (response.success) {
761 if (response.data.status == "success") {
762 $("body").trigger("update_checkout");
763 } else {
764 console.log(response);
765 }
766 } else {
767 console.log(response);
768 }
769 },
770 });
771 });
772
773 $(document).on("change", ".wpfnl-variation", function (e) {
774 e.preventDefault();
775 let ajaxurl = window.wpfnl_obj.ajaxurl,
776 step_id = window.wpfnl_obj.step_id,
777 attrs = [],
778 quantity = $(this).parents(".offer-variation-wrapper").find(".wpfnl-varition-qty").val(),
779 product_id = $(this).parents(".offer-variation-wrapper").find(".wpfnl-product-id").val(),
780 load_ajax = true;
781
782 $(this)
783 .parents(".offer-variation-wrapper")
784 .find(".wpfnl-variable-attribute-offer")
785 .each(function () {
786 var title = $(this).attr("id");
787 var value = $(this).val();
788 if (value) {
789 attrs.push({ [title]: value });
790 } else {
791 load_ajax = false;
792 }
793 });
794
795 if (load_ajax) {
796 $("input[name='_wpfunnels_variable_product']").val('selected');
797 jQuery.ajax({
798 type: "POST",
799 url: ajaxurl,
800 data: {
801 action: "wpfnl_update_variable_ajax",
802 quantity: quantity,
803 step_id: step_id,
804 product_id: product_id,
805 attrs: attrs,
806 },
807 success: function (response) {
808 if (response.success) {
809 if (response.data.status == "success") {
810 $("body").trigger("update_checkout");
811 }
812 } else {
813 console.log(response);
814 }
815 },
816 });
817 }else{
818 $("input[name='_wpfunnels_variable_product']").val('unselected');
819 }
820 });
821
822 $(document).on("keyup change input", ".wpfnl-quantity-setect", function (e) {
823 e.preventDefault();
824 var ajaxurl = window.wpfnl_obj.ajaxurl;
825 var step_id = window.wpfnl_obj.step_id;
826 var product_id = $(this).data("product-id");
827 var variation_id = $(this).data("variation-id");
828 var variation = $(this).data("variation");
829 var set_quantity = $(this).data("set-quantity");
830 var quantity = parseInt($(this).val());
831 if( set_quantity == 'yes' ){
832 var limit = parseInt($(this).data("quantity-limit"));
833
834 if( quantity > limit ){
835 quantity = limit;
836 $(this).val(quantity)
837 }
838 }
839 var quantity = $(this).val();
840 $("input[name='_wpfunnels_select_quantity']").val('yes');
841 if (!quantity) {
842 return false;
843 }
844
845 if (quantity < 1 ) {
846 quantity = 1;
847 $(this).val(quantity)
848 }
849
850 let that = this;
851 jQuery.ajax({
852 type: "POST",
853 url: ajaxurl,
854 data: {
855 action: "wpfnl_update_quantity_ajax",
856 quantity: quantity,
857 step_id: step_id,
858 product_id: product_id,
859 variation_id: variation_id,
860 variation: variation,
861 },
862 success: function (response) {
863 if (response.success) {
864 $(that).val(quantity);
865 // Clear existing notices only within the checkout form
866 $('.woocommerce-checkout .woocommerce-error, .woocommerce-checkout .woocommerce-message').remove();
867 var checkoutForm = $('.woocommerce-checkout');
868 if (response.data.status == "success") {
869 $("body").trigger("update_checkout");
870 }else{
871 if (checkoutForm.find('.woocommerce-error').length === 0) {
872 checkoutForm.prepend('<div class="woocommerce-error">' + response.data.message + '</div>');
873 }
874 }
875 }
876 },
877 });
878 });
879
880
881 if ("1" !== window?.wpfnl_obj?.is_builder_preview) {
882 wpfunnels_facebook_pixel();
883 }
884 });
885
886 // trigger ajax if any user abandon the funnel
887 var doAjaxBeforeUnloadEnabled = true;
888 var doAjaxBeforeUnload = function (evt) {
889 if (!doAjaxBeforeUnloadEnabled) {
890 return;
891 }
892 doAjaxBeforeUnloadEnabled = false;
893 var ajaxurl = window.wpfnl_obj.ajaxurl;
894 jQuery.ajax({
895 type: "POST",
896 url: ajaxurl,
897 data: {
898 action: "maybe_abandoned_funnel",
899 funnel_id: window.wpfnl_obj.funnel_id,
900 step_id: window.wpfnl_obj.step_id,
901 security: window.wpfnl_obj.abandoned_ajax_nonce,
902 },
903 success: function (response) {},
904 });
905 };
906
907 // $(document).ready( function () {
908 // window.onbeforeunload = doAjaxBeforeUnload;
909
910 // checkStepBouncing();
911 // });
912
913 const checkStepBouncing = () => {
914 const wpfnlBody = $( 'body.single-wpfunnel_steps' )?.length;
915
916 if ( wpfnlBody ) {
917 let time = 0;
918
919 const bounceTimeInterval = setInterval( function () {
920 time++;
921 if ( 10 <= time ) {
922 setStepBounce(time, bounceTimeInterval);
923 }
924 }, 1000 );
925
926 $( 'input, button, a, span' ).on( 'click change', () => {
927 if ( 10 >= time ) {
928 setStepBounce( time, bounceTimeInterval )
929 }
930 } );
931 }
932 }
933 const setStepBounce = ( time, bounceTimeInterval ) => {
934 const ajaxurl = window?.wpfnl_pro_obj?.ajaxurl;
935 const data = {
936 action: 'wpfnl_set_bounce_rate',
937 security: window?.wpfnl_pro_obj?.ajaxNonce,
938 funnel_id: window?.wpfnl_pro_obj?.funnel_id,
939 step_id: window?.wpfnl_pro_obj?.step_id,
940 user_id: window?.wpfnl_pro_obj?.user_id
941 };
942
943 $.ajax({
944 url: ajaxurl,
945 type: "POST",
946 data: data,
947 dataType: "json",
948 success: function (data) {
949 clearInterval( bounceTimeInterval );
950 },
951 error: function (error) {
952 clearInterval( bounceTimeInterval );
953 }
954 });
955 }
956 })(jQuery);
957