PageSourceSearch

https://www.3chi.com/wp-content/plugins/free-gifts-for-woocommerce/assets/js/frontend.js?ver=13.7.0

js 3chi.com collected 2026-09-25 23:51:42 UTC 16,112 bytes, 456 lines download raw bytes

1/* global fgf_frontend_params, lightcase */
2
3jQuery(function ($) {
4	'use strict';
5
6	if (typeof fgf_frontend_params === 'undefined') {
7		return false;
8	}
9
10	var FGF_Frontend = {
11		stopped_place_order:true,
12		init: function ( ) {
13			$(document).on('click', '.fgf_pagination', this.manual_gift_pagination);
14			// Block the manual gift products.
15			$(document).on('click', '.fgf-add-manual-gift-product', this.block_manual_gift_products);
16			// Add a gift product via ajax.
17			$(document).on('click', '.fgf-add-manual-gift-product', this.add_gift_product_ajax);
18			// Add the gift product via dropdown.
19			$(document).on('click', '.fgf-add-gift-product', this.add_manually_gift_product);
20			// Show the dropdown options.
21			$(document).on('click', '.fgf-dropdown-default-option-wrapper', this.toggle_dropdown_options);
22			// Select the dropdown selection option.
23			$(document).on('click', '.fgf-dropdown-option-content', this.select_dropdown_option);
24			// Hide the dropdown options when click outside the element.
25			$(document).on('click', this.hide_dropdown_option);
26			// Add the gift product via dropdown.
27			$(document).on('change', '.fgf-gift-product-selection', this.handle_automatic_gift_product);
28			// Add the gift product via dropdown.
29			$(document).on('change', '.fgf-product-variations', this.handle_variation_gift_product);
30			//Update the cart when updating shipping.
31			$(document.body).on('updated_shipping_method', this.updated_shipping_method);
32			// Initialize the third party library after cart updated.
33			$(document.body).on('updated_wc_div', this.reinitialize_require_library);
34			// Update gift details in the checkout page after updated checkout.
35			$(document.body).on('updated_checkout', this.update_gift_details_in_checkout);
36
37			// Show variation data.
38			$(document).on('show_variation', this.onFoundVariation);
39			$(document).on('hide_variation', this.onResetVariation);
40
41			// Prevent place order.
42			$(document).on('click','#place_order', this.prevent_place_order);
43			$(document.body).on('fgf_lightcase_closed', this.maybe_trigger_place_order);
44
45		}, onFoundVariation: function (evt, variation, purchasable) {
46			FGF_Frontend.onResetVariation();
47
48			if (variation.fgf_free_gifts) {
49				var notices_wrapper=$('.fgf-bogo-single-product-eligible-notices-wrapper');
50				if (notices_wrapper.find('.fgf-variation-bogo-eligible-notices-wrapper').length) {
51					notices_wrapper.find('.fgf-variation-bogo-eligible-notices-wrapper').html(variation.fgf_free_gifts);
52				} else {
53					$('<div />', { class: 'fgf-variation-bogo-eligible-notices-wrapper' }).appendTo('.fgf-bogo-single-product-eligible-notices-wrapper');
54					notices_wrapper.find('.fgf-bogo-eligible-notice').hide();
55					notices_wrapper.find('.fgf-variation-bogo-eligible-notices-wrapper').html(variation.fgf_free_gifts);
56				}
57			}
58
59		}, onResetVariation: function (evt) {
60			var notices_wrapper=$('.fgf-bogo-single-product-eligible-notices-wrapper');
61			notices_wrapper.find('.fgf-variation-bogo-eligible-notices-wrapper').remove();
62			notices_wrapper.find('.fgf-bogo-eligible-notice').show();
63		},updated_shipping_method: function ( ) {
64			console.log('');
65			$(document.body).trigger('wc_update_cart');
66		}, reinitialize_require_library: function () {
67			$(document.body).trigger('fgf-enhanced-carousel');
68		}, update_gift_details_in_checkout: function (e, data) {
69			if (data && data.fragments) {
70				if (data.fragments.fgf_notices_html) {
71					$('#fgf-checkout-gift-notices-wrapper').replaceWith(data.fragments.fgf_notices_html);
72				}
73
74				if (data.fragments.fgf_gift_details_html) {
75					$('#fgf-checkout-gift-details-wrapper').replaceWith(data.fragments.fgf_gift_details_html);
76					FGF_Frontend.reinitialize_require_library();
77				}
78
79				if (data.fragments.fgf_progress_bar_html) {
80					$('#fgf-checkout-progress-bar-wrapper').replaceWith(data.fragments.fgf_progress_bar_html);
81				}
82
83				if (data.fragments.fgf_eligible_popup_modals_html) {
84					$('#fgf-checkout-eligible-popup-modals-wrapper').replaceWith(data.fragments.fgf_eligible_popup_modals_html);
85				}
86			}
87		}, block_manual_gift_products: function (event) {
88			var $this = $(event.currentTarget),
89					wrapper = $this.closest('.fgf_gift_products_wrapper');
90
91			if (isBlockCart() || 'yes' === fgf_frontend_params.ajax_add_to_cart || '2' 
91!== fgf_frontend_params.quantity_field_enabled) {
92				return true;
93			}
94
95			FGF_Frontend.block(wrapper);
96			return true;
97		}, add_gift_product_ajax: function (event) {
98			if (!isBlockCart() && 'yes' !== fgf_frontend_params.ajax_add_to_cart && '2' === fgf_frontend_params.quantity_field_enabled) {
99				return true;
100			}
101
102			event.preventDefault( );
103			var $this = $(event.currentTarget),
104					rule_id = $($this).data('rule_id'),
105					product_id = $($this).data('product_id'),
106					buy_product_id = $($this).data('buy_product_id'),
107					coupon_id = $($this).data('coupon_id'),
108					quantity = 1,
109					reload = ('yes' !== fgf_frontend_params.ajax_add_to_cart && '1' !== fgf_frontend_params.quantity_field_enabled),
110					quantity_field = $($this).closest('.fgf-gift-product-add-to-cart-actions').find('.fgf-gift-product-qty');
111
112			if (quantity_field.length && quantity_field.val()) {
113				quantity = quantity_field.val();
114			}
115
116			FGF_Frontend.add_gift_product($this, product_id, rule_id, buy_product_id, coupon_id, quantity, reload);
117
118		}, add_gift_product: function ($this, product_id, rule_id, buy_product_id, coupon_id, quantity, reload) {
119			var content = $this.closest('.fgf_gift_products_wrapper');
120
121			FGF_Frontend.block(content);
122
123			var data = ({
124				action: 'fgf_add_gift_product',
125				product_id: product_id,
126				rule_id: rule_id,
127				buy_product_id: buy_product_id,
128				coupon_id: coupon_id,
129				quantity: quantity,
130				fgf_security: fgf_frontend_params.gift_product_nonce,
131			});
132
133			$.post(fgf_frontend_params.ajaxurl, data, function (res) {
134				if (true === res.success) {
135					if (reload) {
136						reloadWindow(1);
137					} else if ('popup' == content.find('#fgf_gift_products_type').val()) {
138						if (res.data.reload) {
139							reloadWindow(1);
140						} else {
141							FGF_Frontend.update_gift_products_content( content.find('#fgf_gift_products_popup_location').val());
142							$(document.body).trigger('fgf-enhanced-carousel');
143						}
144					} else {
145						updateCart(1);
146					}
147				} else {
148					alert(res.data.error);
149				}
150
151				FGF_Frontend.unblock(content);
152			});
153		}, update_gift_products_content: function ( popup_location) {
154			var data = ({
155				action: 'fgf_update_gift_products_content',
156				popup_location:popup_location,
157				fgf_security: fgf_frontend_params.gift_product_nonce,
158			});
159			$.ajax({
160				type: 'POST',
161				url: fgf_frontend_params.ajaxurl,
162				data: data,
163				async: false,
164				dataType: 'html',
165				success: function (response) {
166					var html = $.parseHTML(response);
167					$('.fgf-gift-products-content').replaceWith($('.fgf-gift-products-content', html));
168					$(document).trigger('fgf_create_cart_notice');
169					
170					lightcase.settings.onClose = {
171						foo: function () {
172							window.location.reload();
173						}
174					};
175				}
176			});
177		}, handle_variation_gift_product: function (event) {
178
179			var $this = $(event.currentTarget),
180					product_id = $($this).val( ),
181					url = fgf_frontend_params.add_to_cart_link,
182					link = $($this).closest('span').find('.fgf-add-manual-gift-product');
183
184			// Create a add to cart link.
185			url = url.replace('%s', product_id);
186			url = url.replace('%s', $($this).data('rule_id'));
187			url = url.replace('%s', $($this).data('buy_product_id'));
188			url = url.replace('%s', $($this).data('coupon_id'));
189			link.attr('href', url);
190			link.data('product_id', product_id);
191			link.data('rule_id', $($this).data('rule_id'));
192			link.data('buy_product_id', $($this).data('buy_product_id'));
193			link.data('coupon_id', $($this).data('coupon_id'));
194
195			$($this).closest('.fgf-gift-product-item').find('.fgf-product-image').html($(this).find(':selected').data('image'));
196
197			$(document.body).trigger('fgf_updated_gift_variation_product');
198		}, toggle_dropdown_options: function (event) {
199			event.preventDefault( );
200
201			$('.fgf-dropdown-options-wrapper').toggle();
202
203		}, select_dropdown_option: function (event) {
204			event.preventDefault( );
205			var $this = $(event.currentTarget),
206					content = $this.find('.fgf-dropdown-option');
207
208			$('.fgf-dropdown-option-selected').html(content.html());
209			$('.fgf-dropdown-options-wrapper').toggle();
210
211			var rule_id = $($this).data('rule-id'),
212					buy_product_id = $($this).data('buy-product-id'),
213					coupon_id = $($this).data('coupon-id');
214
215			// Add a gift automatically.
216			if ('2' === fgf_frontend_params.dropdown_add_to_cart_behaviour) {
217				// The eligible gifts popup is a display-only preview, so never add the
218				// gift to the cart when an option is selected there.
219				if ($this.closest('.fgf-eligible-gifts-modal').length) {
220					return;
221				}
222				FGF_Frontend.block($this.closest('.fgf-dropdown-wrapper'));
223				FGF_Frontend.add_gift_product_automatically($this, $($this).val(), rule_id, buy_product_id, coupon_id);
224			} else {
225				$('.fgf-gift-product-selection')
226						.val($($this).val()).attr('data-rule-id', rule_id)
227						.attr('data-buy-product-id', buy_product_id)
228						.attr('data-coupon-id', coupon_id);
229			}
230
231		}, hide_dropdown_option: function (event) {
232			var wrapper = $('.fgf-dropdown-wrapper');
233
234			// if the target of the click isn't the container nor a descendant of the container
235			if (!wrapper.is(event.target) && wrapper.has(event.target).length === 0) {
236				$('.fgf-dropdown-options-wrapper').hide();
237			}
238
239		}, add_manually_gift_product: function (event) {
240			event.preventDefault( );
241			// Return if the automatic add to cart mode is enabled.
242			if ('2' === fgf_frontend_params.dropdown_add_to_cart_behaviour) {
243				return false;
244			}
245
246			var $this = $(event.currentTarget),
247					wrapper = $this.closest('.fgf-gift-product-wrapper'),
248					product_id = wrapper.find('.fgf-gift-product-selection').val( );
249
250			if ('' === product_id || '0' === product_id) {
251				alert(fgf_frontend_params.add_to_cart_alert_message);
252				return false;
253			}
254
255			if ('2' === fgf_frontend_params.dropdown_display_type) {
256				var option_wrapper = wrapper.find('.fgf-gift-product-selection');
257			} else {
258				var option_wrapper = wrapper.find('.fgf-gift-product-selection').find(':selected');
259			}
260
261			var rule_id = option_wrapper.data('rule-id'),
262					buy_product_id = option_wrapper.data('buy-product-id'),
263					coupon_id = option_wrapper.data('coupon-id');
264
265			if ('yes' === fgf_frontend_params.ajax_add_to_cart) {
266				FGF_Frontend.add_gift_product($this, product_id, rule_id, buy_product_id, coupon_id, 1, false);
267			} else {
268				FGF_Frontend.add_gift_product_automatically($this, product_id, rule_id, buy_product_id, coupon_id);
269			}
270
271		}, handle_automatic_gift_product: function (event) {
272			event.preventDefault( );
273			// Return if the automatic add to cart is disbaled. 
274			if ('2' !== fgf_frontend_params.dropdown_add_to_cart_behaviour) {
275				return false;
276			}
277
278			var $this = $(event.currentTarget);
279
280			// The eligible gifts popup is a display-only preview, so never add the
281			// gift to the cart when an option is selected there.
282			if ($($this).closest('.fgf-eligible-gifts-modal').length) {
283				return false;
284			}
285
286			var rule_id = $($this).find(':selected').data('rule-id'),
287					buy_product_id = $($this).find(':selected').data('buy-product-id'),
288					coupon_id = $($this).find(':selected').data('coupon-id');
289
290			FGF_Frontend.add_gift_product_automatically($this, $($this).val(), rule_id, buy_product_id, coupon_id);
291
292		}, add_gift_product_automatically: function ($this, product_id, rule_id, buy_product_id, coupon_id) {
293			// Return if the product ID does not exists. 
294			if ('' === product_id) {
295				return false;
296			}
297
298			if (isBlockCart()) {
299				FGF_Frontend.add_gift_product($this, product_id, rule_id, buy_product_id, coupon_id, 1, false);
300			} else {
301				// Create a add to cart link.
302				var url = fgf_frontend_params.add_to_cart_link;
303				url = url.replace('%s', product_id);
304				url = url.replace('%s', rule_id);
305				url = url.replace('%s', buy_product_id);
306				url = url.replace('%s', coupon_id);
307
308				// Redirect to add the gift product to the cart.
309				window.location.href = url;
310			}
311		}, manual_gift_pagination: function (event) {
312			event.preventDefault( );
313			var $this = $(event.currentTarget),
314					gift_wrapper = $this.closest('#fgf_gift_products_wrapper'),
315					current_page = $this.data('page'),
316					display_type=gift_wrapper.find('#fgf_gift_products_display_type').val(),
317					popup_location=gift_wrapper.find('#fgf_gift_products_popup_location').val(),
318					rule_id=gift_wrapper.find('#fgf_gift_products_rule_id').val();
319
320			if('4' === display_type || 'grid' === display_type){
321				var wrapper = $this.closest('.fgf-gift-products-content'),
322				items_wrapper = wrapper.find('.fgf-gift-products-grid-items');
323			}else{
324				var wrapper = $this.closest('table.fgf_gift_products_table'),
325				items_wrapper = wrapper.find('tbody');
326			}
327
328			FGF_Frontend.block(items_wrapper);
329
330			var data = ({
331				action: 'fgf_gift_products_pagination',
332				page_number: current_page,
333				page_url: fgf_frontend_params.current_page_url,
334				display_type:display_type,
335				popup_location:popup_location,
336				rule_id:rule_id,
337				fgf_security: fgf_frontend_params.gift_products_pagination_nonce,
338			});
339
340			$.post(fgf_frontend_params.ajaxurl, data, function (res) {
341
342				if (true === res.success) {
343					items_wrapper.html(res.data.html);
344					wrapper.find('.fgf_pagination').removeClass('current');
345					wrapper.find('.fgf_pagination_' + current_page).addClass('current');
346					var next_page = current_page;
347					if (current_page > 1) {
348						next_page = current_page - 1;
349					}
350
351					var last_page = wrapper.find('.fgf_last_pagination').data('page');
352					if (current_page < last_page) {
353						last_page = current_page + 1;
354					}
355
356					wrapper.find('.fgf_next_pagination').data('page', last_page);
357					wrapper.find('.fgf_prev_pagination').data('page', next_page);
358
359					$(document.body).trigger('fgf_updated_pagination');
360
361				} else {
362					alert(res.data.error);
363				}
364
365				FGF_Frontend.unblock(items_wrapper);
366			}
367			);
368		},
369		/**
370		 * Prevent the placing the order in the checkout when the remaining gifts are available.
371		 * 
372		 * @since 12.4.0
373		 * @param {object} event 
374		 * @returns bool
375		 */
376		prevent_place_order:function(event){
377			var valid_types=['2','3'];
378			if(!valid_types.includes(fgf_frontend_params.force_checkout_type)){
379			  return true;
380			}
381
382			if(!FGF_Frontend.stopped_place_order || !$('.fgf-popup-force-checkout-gifts').length){
383				return true;
384			} 
385
386			event.preventDefault( );
387			$('.fgf-popup-force-checkout-gifts').click();
388			FGF_Frontend.stopped_place_order=false;
389
390			return false;
391		}, 
392		/**
393		 * May be trigger the place order button once the user skip the gifts.
394		 * 
395		 * @since 12.4.0
396		 */
397		maybe_trigger_place_order:function(){
398			// Trigger exclusively once the forced gifts popup is closed.
399			if(!FGF_Frontend.stopped_place_order){
400				$('#place_order').click();
401			}
402		},  block: function (id) {
403			$(id).block({
404				message: null,
405				overlayCSS: {
406					background: '#fff',
407					opacity: 0.7
408				}
409			});
410		}, unblock: function (id) {
411			$(id).unblock( );
412		}
413	};
414
415	/**
416	 * Is block cart/checkout.
417	 * 
418	 * @since 11.0.0
419	 * @returns {boolean}
420	 */
421	function isBlockCart() {
422		return fgf_frontend_params.is_block_cart || fgf_frontend_params.is_block_checkout;
423	}
424
425	/**
426	 * Reload the window.
427	 * 
428	 * @since 11.0.0
429	 * @returns {undefined}
430	 */
431	function reloadWindow(action) {
432		if (isBlockCart()) {
433			$(document.body).trigger('fgf_update_cart_block', action);
434			$(document.body).trigger('fgf_close_lightcase');
435		} else {
436			window.location.reload();
437		}
438	}
439
440	/**
441	 * Update the cart after any action done.
442	 * 
443	 * @since 11.0.0
444	 * @returns {undefined}
445	 */
446	function updateCart(action) {
447		if (isBlockCart()) {
448			$(document.body).trigger('fgf_update_cart_block', action);
449		} else {
450			$(document.body).trigger('wc_update_cart');
451			$(document.body).trigger('update_checkout');
452		}
453	}
454
455	FGF_Frontend.init( );
456});

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.