PageSourceSearch

https://werens.com/wp-content/plugins/woo-variations-table-grid/assets/js/add-to-cart.js?ver=1.4.5

js werens.com collected 2026-09-24 19:32:58 UTC 18,427 bytes, 643 lines download raw bytes

1jQuery(document).ready(function($) {
2
3	var ajaxurl = localvars.ajax_url;
4	var carturl = localvars.cart_url;
5	var currency_symbol = localvars.currency_symbol;
6	var thousand_separator = localvars.thousand_separator;
7	var decimal_separator = localvars.decimal_separator;
8	var decimal_decimals = localvars.decimal_decimals;
9	var currency_pos = localvars.currency_pos;
10	var price_display_suffix = localvars.price_display_suffix;
11	var lightbox = localvars.lightbox;
12	var gclicked = 0;
13	var glob_clicked = 0;
14	var vartable_ajax = localvars.vartable_ajax;
15	var $fragment_refresh = '';
16	var count = 0;
17	var numofadded = 0;
18	var requests_done = 0;
19
20	var formdata = new Array;
21
22
23	if (lightbox == 1) {
24		jQuery(".vartable_zoom").fancybox({
25			loop: false,
26			animationDuration: 250,
27			thumbs: {
28				autoStart: true
29			}
30		});
31	}
32
33	/** Cart Handling */
34	$supports_html5_storage = ('sessionStorage' in window && window['sessionStorage'] !== null);
35	if (vartable_ajax == 1) {
36		$fragment_refresh = {
37			url: ajaxurl,
38			// url: woocommerce_params.ajax_url,
39			type: 'POST',
40			data: {
41				action: 'woocommerce_get_refreshed_fragments'
42			},
43			success: function(data) {
44				if (data && data.fragments) {
45
46					$.each(data.fragments, function(key, value) {
47						$(key).replaceWith(value);
48					});
49
50					if ($supports_html5_storage) {
51						sessionStorage.setItem("wc_fragments", JSON.stringify(data.fragments));
52						sessionStorage.setItem("wc_cart_hash", data.cart_hash);
53					}
54					console.log('refresh');
55					$('body').trigger('wc_fragments_refreshed');
56				}
57			}
58		};
59	}
60
61
62
63	if (jQuery(".vt_notification").length > 0) {
64		jQuery('.vt_notification .slideup_panel').on('click', function(event) {
65
66			event.preventDefault();
67			$( this ).closest( '.vt_notification' ).stop(true).slideUp(200);
68			glob_clicked = 0;
69
70			return false;
71		});
72	}
73
74	function vartable_init() {
75		if (jQuery("table.vartable").length > 0) {
76			
77			
78			if ( $( '.vartable .swatch-wrapper a' ).length > 0 ) {
79				$( '.vartable .swatch-wrapper a' ).on( 'click', function( e ) {
80					
81					e. preventDefault();
82					return false;
83					
84				});
85			}
86			
87			jQuery("table.vartable").each(function(index) {
88
89				var vartable = jQuery(this);
90				var random_id = jQuery(this).data('random');
91				var vartable_ajax = jQuery(this).data('vartable_ajax');
92				var cartredirect = jQuery(this).data('cartredirect');
93				var sorting = jQuery(this).data('sort');
94				var vartable_globalcart = jQuery(this).data('globalcart');
95				var preorder = jQuery(this).data('preorder');
96				var preorder_direction = jQuery(this).data('preorder_direction');
97
98
99				update_global_sum(jQuery(this).find('input[name="var_quantity"]'));
100
101				// gift wrap
102				if (vartable.find("input.var_gift_wrap").length > 0) {
103					jQuery(document).on("change", vartable.find("input.var_gift_wrap"), function() {
104						if (jQuery(this).is(":checked")) {
105							jQuery(this).closest('tr').find(".cartcol input.gift_wrap").val("yes");
106						} else {
107							jQuery(this).closest('tr').find(".cartcol input.gift_wrap").attr("value", "");
108						}
109					});
110				}
111
112
113				if (vartable.find("div.qtywrap").length > 0) {
114
115
116
117
118					jQuery("#tb_" + random_id + " div.qtywrap").each(function() {
119						var qtythis = jQuery(this);
120						qtythis.find(".minusqty").on("click", function() {
121							var valnum = parseInt(qtythis.find("input").val());
122							var valmin = qtythis.find("input").attr('min');
123
124							if (typeof valmin === 'undefined' || valmin === null) {
125								valmin = 0;
126							}
127
128							if (qtythis.find("input").attr("step") && qtythis.find("input").attr("step") > 0) {
129								var step = parseInt(qtythis.find("input").attr("step"));
130							} else {
131								var step = 1;
132							}
133
134							if (valnum - step >= valmin) {
135								qtythis.find("input").val(valnum - step);
136								qtythis.closest('tr').find(".cartcol input.hidden_quantity").val(valnum - step);
137								qtythis.closest('tr').find(".totalcol").text(get_price_html((valnum - step) * qtythis.closest('tr').data('price')));
138								qtythis.find("input").trigger("qty:change");
139							}
140						});
141						qtythis.find(".plusqty").on("click", function() {
142							var valnum = parseInt(qtythis.find("input").val());
143							var valmax = qtythis.find("input").attr('max');
144
145							if (typeof valmax === 'undefined' || valmax === null) {
146								valmax = -1;
147							}
148
149							if (qtythis.find("input").attr("step") && qtythis.find("input").attr("step") > 0) {
150								var step = parseInt(qtythis.find("input").attr("step"));
151							} else {
152								var step = 1;
153							}
154							if ((valnum + step <= valmax) || valmax == -1) {
155								qtythis.find("input").val(valnum + step);
156								qtythis.closest('tr').find(".cartcol input.hidden_quantity").val(valnum + step);
157								qtythis.closest('tr').find(".totalcol").text(get_price_html((valnum + step) * qtythis.closest('tr').data('price')));
158								qtythis.find("input").trigger("qty:change");
159							}
160						});
161					});
162				}
163
164
165				if (sorting == 'yes') {
166
167					var $table = jQuery("#tb_" + random_id + "").stupidtable();
168					if (preorder != '' && preorder != 'custom') {
169						var $th_to_sort = $table.find("thead th." + preorder);
170						$th_to_sort.stupidsort();
171					}
172					if ($th_to_sort !== undefined && $th_to_sort !== null) {
173						if (preorder_direction != '' && preorder_direction != 'custom') {
174							$th_to_sort.stupidsort(preorder_direction);
175						} else {
176							$th_to_sort.stupidsort('asc');
177						}
178					}
179				}
180
181
182
183
184				jQuery(document).on("click", "a#gc_" + random_id + "_top", function(event) {
185					gclicked = 1;
186				});
187				jQuery(document).on("click", "a#gc_" + random_id + "_bottom", function(event) {
188					gclicked = 1;
189				});
190			});
191
192			jQuery(document).on("submit", "form.vtajaxform", function(event) {
193
194				event.preventDefault();
195
196				console.log("triggered");
197
198				if (jQuery(this).find(".hidden_quantity").val() > 0) {
199
200					// reset the formdata array
201					formdata = [];
202					formdata.length = 0;
203					$formdata = get_form_data(jQuery(this));
204
205
206					if (jQuery('#add2cartbtn_' + formdata['thisbuttonid'][1]).length > 0) {
207						jQuery('#add2cartbtn_' + formdata['thisbuttonid'][1]).attr('disabled', 'disabled');
208						jQuery('#add2cartbtn_' + formdata['thisbuttonid'][1]).addClass('working');
209						jQuery(".vtspinner_" + formdata['thisbuttonid'][1]).fadeIn(200);
210					}
211
212
213					vartable_request(formdata);
214
215
216				}
217
218				return false;
219
220			});
221
222			if (jQuery(".vartable_gc_wrap_bottom .globalcartbtn.submit, .vartable_gc_wrap_top .globalcartbtn.submit").length > 0) {
223				jQuery(document).on("click", ".vartable_gc_wrap_bottom .globalcartbtn.submit:not(.working), .vartable_gc_wrap_top .globalcartbtn.submit:not(.working)", function(event) {
224
225
226					event.preventDefault();
227					glob_clicked = 1;
228					gclicked = 1;
229					numofadded = 0;
230					requests_done = 0;
231
232					if (jQuery(this).hasClass('working')) {
233						return false;
234					}
235
236					var clickthis = jQuery(this);
237					var pid = clickthis.attr("id").split("_");
238
239					var vartable = jQuery('#tb_' + pid[1]);
240					var random_id = vartable.data('random');
241					var vartable_ajax = vartable.data('vartable_ajax');
242					var cartredirect = vartable.data('cartredirect');
243					var sorting = vartable.data('sort');
244					var vartable_globalcart = vartable.data('globalcart');
245					var preorder = vartable.data('preorder');
246					var preorder_direction = vartable.data('preorder_direction');
247					var position = jQuery(this).data('position');
248
249					clickthis.addClass('working').attr('disabled', 'disabled');
250
251					jQuery(".vtspinner_" + position + ".vtspinner_" + pid[1]).stop(true).fadeIn(100, function() {
252
253						count = 0;
254						jQuery("table#tb_" + random_id + " tr").not(".descrow").each(function(index) {
255							if (jQuery(this).find("input.globalcheck").length > 0 && jQuery(this).find("input.globalcheck").is(":checked") && jQuery(this).find("input.hidden_quantity").val() > 0 && jQuery(this).data('price') != '') {
256
257								count = count + 1;
258							}
259						});
260
261						if (count == 0) {
262							clickthis.removeClass('working').prop("disabled", false);
263							jQuery(".vtspinner_" + position + ".vtspinner_" + pid[1]).stop(true).fadeOut(100);
264							return false;
265						}
266
267
268						var trig = 0;
269
270						var promises = [];
271
272
273						jQuery("table#tb_" + random_id + " tr").not(".descrow").each(function(index) {
274							if (jQuery(this).find("input.globalcheck").length > 0 && jQuery(this).find("input.globalcheck").is(":checked") && jQuery(this).find("input.hidden_quantity").val() > 0 && jQuery(this).data('price') != '') {
275
276								// reset the formdata array
277								formdata = [];
278								formdata.length = 0;
279
280								var formobj = jQuery(this).find("form.vtajaxform");
281
282								formdata = get_form_data(formobj);
283
284								vartable_request(formdata);
285
286								requests_done = requests_done + 1
287
288
289								// promises.push(request);
290								// numofadded = numofadded + parseInt(jQuery(this).find("input.hidden_quantity").val());
291
292								trig = 1;
293							}
294
295						});
296
297
298						jQuery(document).on('vartable_global_add_finished', function() {
299							// jQuery.when.apply(null, promises).done(function(){
300
301
302							if (vartable_ajax != 1 || cartredirect == 'yes') {
303								if (count <= 0 && trig == 1) {
304									if (cartredirect == 'yes') {
305										window.location.href = "" + carturl + "";
306									}
307									if (cartredirect == 'no') {
308										location.reload();
309									}
310								}
311							}
312
313							if (count <= 0) {
314								jQuery(".vtspinner_" + pid[1]).stop(true, true).fadeOut(100);
315								if (trig == 1) {
316									jQuery(".added2cartglobal_" + pid[1]).stop(true).fadeIn(200);
317									jQuery(".added2cartglobal_" + pid[1]).delay(3000).fadeOut(200);
318								}
319
320								if (numofadded > 0) {
321									jQuery('#vt_added_to_cart_notification span').text(numofadded);
322									jQuery('#vt_added_to_cart_notification').stop(true).slideDown(100, function() {
323										jQuery('#vt_added_to_cart_notification').delay(6000).slideUp(200);
324										glob_clicked = 0;
325									});
326								}
327
328								glob_clicked = 0;
329								gclicked = 0;
330								clickthis.removeClass('working').prop("disabled", false);
331
332								// alert('All done');
333								console.log('finished');
334							}
335
336							// });
337						});
338
339
340					});
341
342
343
344
345				});
346			}
347
348
349		}
350	}
351
352	if ( $( 'body.theme-betheme' ).length > 0 ) {
353		$( window ).load(function() {
354			vartable_init();
355		});
356	} else {
357		vartable_init();
358	}
359
360	jQuery(document).on("input", ".vartable .qtycol input.input-text.qty", function() {
361
362		var valmin = jQuery(this).attr('min');
363		var valmax = jQuery(this).attr('max');
364
365
366		if (typeof valmin === 'undefined' || valmin === null) {
367			valmin = 0;
368		}
369		if (typeof valmax === 'undefined' || valmax === null) {
370			valmax = -1;
371		}
372
373		if (parseInt(jQuery(this).val()) < valmin) {
374			jQuery(this).val(valmin);
375		}
376		if (parseInt(jQuery(this).val()) > valmax && valmax != -1) {
377			jQuery(this).val(valmax);
378		}
379
380		jQuery(this).closest('tr').find(".totalcol").text(get_price_html(jQuery(this).val() * jQuery(this).closest('tr').data('price')));
381		jQuery(this).closest('tr').find(".cartcol input.hidden_quantity").val(jQuery(this).val());
382		jQuery(this).trigger("qty:change");
383	});
384
385	if (jQuery('.giftcol').length > 0) {
386		jQuery(document).on("change", "input.var_gift_wrap", function() {
387			if (jQuery(this).is(":checked")) {
388				jQuery(this).closest('tr').find(".cartcol input.gift_wrap").val("yes");
389			} else {
390				jQuery(this).closest('tr').find(".cartcol input.gift_wrap").attr("value", "");
391			}
392		});
393	}
394
395
396	jQuery(document).on('qty:change', 'input[name="var_quantity"]', function() {
397		update_global_sum(jQuery(this));
398	});
399	jQuery(document).on('change', 'input.globalcheck', function() {
400		update_global_sum(jQuery(this));
401	});
402
403	// update global cart button count on page load
404
405	if (jQuery('table.vartable').length > 0) {
406		jQuery('table.vartable').each(function() {
407			update_global_sum(jQuery(this));
408		});
409	}
410
411
412
413	function update_global_sum(object) {
414		var random_id = object.closest('table.vartable').data('random');
415		var numofchecked = 0;
416		jQuery("table#tb_" + random_id + "").each(function(index) {
417
418			jQuery(this).find('tr').each(function(row) {
419
420				if (jQuery(this).find('.globalcheck').is(":checked") && jQuery(this).data('price') != '' && parseInt(j
420Query(this).find('input[name="quantity"]').val()) > 0) {
421					numofchecked = numofchecked + parseInt(jQuery(this).find('input[name="quantity"]').val());
422				}
423
424			});
425
426		});
427
428		jQuery('#gc_' + random_id + '_top span.vt_products_count, #gc_' + random_id + '_bottom span.vt_products_count').text(' (' + numofchecked + ')');
429
430	}
431
432	jQuery(document).on("change", ".vartable_selectall_check", function(event) {
433
434		var said = jQuery(this).attr("id").split("_");
435
436		if (this.checked) {
437			jQuery("table#tb_" + said[1] + " tr").each(function(index) {
438
439				if (jQuery(this).find("input.globalcheck").length > 0) {
440
441					jQuery(this).find("input.globalcheck").prop("checked", 'checked');
442				}
443
444			});
445		} else {
446			jQuery("table#tb_" + said[1] + " tr").each(function(index) {
447
448				if (jQuery(this).find("input.globalcheck").length > 0) {
449					jQuery(this).find("input.globalcheck").prop("checked", false);
450				}
451
452			});
453		}
454
455		update_global_sum(jQuery(this));
456	});
457
458	Number.prototype.formatMoney = function(c, d, t) {
459		var n = this,
460			c = isNaN(c = Math.abs(c)) ? 2 : c,
461			d = d == undefined ? "." : d,
462			t = t == undefined ? "," : t,
463			s = n < 0 ? "-" : "",
464			i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "",
465			j = (j = i.length) > 3 ? j % 3 : 0;
466		return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");
467	};
468
469	function get_price_html(price) {
470		price = parseFloat(price).formatMoney(decimal_decimals, decimal_separator, thousand_separator);
471
472
473
474		if (currency_pos == 'left') {
475			price = currency_symbol + price;
476		}
477		if (currency_pos == 'right') {
478			price = price + currency_symbol;
479		}
480		if (currency_pos == 'left_space') {
481			price = currency_symbol + ' ' + price;
482		}
483		if (currency_pos == 'right_space') {
484			price = price + ' ' + currency_symbol;
485		}
486
487		if (price_display_suffix != '') {
488			price = price + ' ' + price_display_suffix;
489		}
490
491		return price;
492	}
493
494
495	function get_form_data(formobj) {
496
497
498		formdata['variation_id'] = formobj.find('input[name="variation_id"]').val();
499		formdata['product_id'] = formobj.find('input[name="product_id"]').val();
500		formdata['quantity'] = formobj.find('input[name="quantity"]').val();
501		formdata['gift_wrap'] = formobj.find('input[name="gift_wrap"]').val();
502		formdata['variations_json'] = formobj.find('input[name="form_vartable_attribute_json"]').val();
503		formdata['variations'] = formobj.find('input[name="form_vartable_attribute_array"]').val();
504
505		formdata['vartable_ajax'] = formobj.closest('table.vartable').data('vartable_ajax');
506		formdata['vartable_globalcart'] = formobj.closest('table.vartable').data('globalcart');
507		formdata['cartredirect'] = formobj.closest('table.vartable').data('cartredirect');
508
509		formdata['addvtdata'] = formobj.serialize();
510		formdata['thisid'] = formobj.attr("data-variation_id");
511		formdata['thisbutton'] = formobj.find('button.add_to_cart');
512		formdata['thisbuttonid'] = formdata['thisbutton'].attr('id').split('_');
513
514		// console.log(formdata);
515
516		return formdata;
517
518	}
519
520	function vartable_request(formdata) {
521
522		jQuery.ajaxQueue({
523			type: "POST",
524			url: ajaxurl,
525			data: {
526				"action": "add_variation_to_cart",
527				"product_id": formdata['product_id'],
528				"variation_id": formdata['variation_id'],
529				"variations": formdata['variations'],
530				"quantity": formdata['quantity'],
531				"gift_wrap": formdata['gift_wrap']
532			},
533			success: function(data) {
534				
535				console.log('data');
536				console.log( data );
537
538
539				//conditionally perform fragment refresh
540				if (formdata['vartable_ajax'] == 1) {
541					$.ajax($fragment_refresh);
542				}
543				
544				var $animated_message = formdata['quantity'];
545				
546				if ( data.error == true && data.error_message !== undefined && data.error_message != '' ) {
547					
548					jQuery('#vt_error_notification span').html( data.error_message );
549					jQuery('#vt_error_notification').stop(true, true).slideDown(200, function() {
550						jQuery('#vt_error_notification').delay(3000).slideUp(200, function() {
551							jQuery('#vt_error_notification span').html( '' );
552						});
553					});
554					
555				} else {
556					
557					numofadded = parseInt( formdata['quantity'] ) + parseInt( numofadded );
558				
559					if (glob_clicked == 0) {
560						jQuery('#vt_added_to_cart_notification span').text( formdata['quantity'] );
561						jQuery('#vt_added_to_cart_notification').stop(true, true).slideDown(200, function() {
562							jQuery('#vt_added_to_cart_notification').delay(3000).slideUp(200);
563						});
564					}
565				}
566
567				// set counter to track when all requests are done
568				count = count - 1;
569
570				//console.log(count);
571
572				jQuery(".vtspinner_" + formdata['thisbuttonid'][1]).fadeOut(200, function() {
573
574					if (count <= 0) {
575						if (glob_clicked == 1 || gclicked == 1) {
576							jQuery('body').trigger('vartable_global_add_finished');
577							glob_clicked = 0;
578							gclicked = 0;
579						}
580					}
581
582				});
583
584				jQuery("#added2cart_" + formdata['thisbuttonid'][1]).fadeIn(200, function() {
585					jQuery("#added2cart_" + formdata['thisbuttonid'][1]).delay(1000).fadeOut(1000);
586
587					if (jQuery('#add2cartbtn_' + formdata['thisbuttonid'][1]).length > 0) {
588						jQuery('#add2cartbtn_' + formdata['thisbuttonid'][1]).prop("disabled", false);
589						jQuery('#add2cartbtn_' + formdata['thisbuttonid'][1]).removeClass('working');
590					}
591
592
593					if ((formdata['vartable_ajax'] != 1) || formdata['cartredirect'] == 'yes') {
594						if (gclicked == 0) {
595							if (formdata['cartredirect'] == 'yes') {
596								window.location.href = "" + carturl + "";
597							}
598							if (formdata['cartredirect'] == 'no') {
599								location.reload();
600							}
601						}
602					}
603
604				});
605			},
606			error: function(data) {
607				console.log('error');
608				console.log(data);
609			}
610		});
611
612	}
613
614
615	// reload for yith quick view
616	jQuery(document).on('qv_loader_stop', function() {
617		vartable_init();
618	});
619
620	(function($) {
621
622		var ajaxQueue = $({});
623
624		$.ajaxQueue = function(ajaxOpts) {
625
626			var oldComplete = ajaxOpts.complete;
627
628			ajaxQueue.queue(function(next) {
629
630				ajaxOpts.complete = function() {
631					if (oldComplete) oldComplete.apply(this, arguments);
632
633					next();
634				};
635
636				$.ajax(ajaxOpts);
637			});
638		};
639
640	})(jQuery);
641
642
643});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.