PageSourceSearch

https://tcc-tuning.ru/wp-content/plugins/woocommerce-add2cart-grampus/assets/js/add2cart.js?ver=1749015926

js tcc-tuning.ru collected 2026-10-08 16:22:54 UTC 10,259 bytes, 389 lines download raw bytes

1/* global wc_add_to_cart_params */
2jQuery( function( $ ) {
3
4	if ( typeof wc_add_to_cart_params === 'undefined' ) {
5		return false;
6	}
7
8	/**
9	 * Add2CartHandler class.
10	 */
11	var Add2CartHandler = function() {
12		var self = this;
13
14		self.requests    = [];
15		self.addRequest  = self.addRequest.bind( self );
16		self.run         = self.run.bind( self );
17		self.modal       = null;
18
19		$( document.body )
20			.on( 'click', '.add_to_cart_button', { Add2CartHandler: self }, self.onAddToCart )
21			.on( 'click', '.remove_from_cart_button', { Add2CartHandler: self }, self.onRemoveFromCart )
22			.on( 'added_to_cart', self.updateButton )
23			.on( 'ajax_request_not_sent.adding_to_cart', self.updateButton )
24			.on( 'added_to_cart_proxy removed_from_cart_proxy', { Add2CartHandler: self }, self.updateFragments )
25			.on( 'submit', 'form.cart', { Add2CartHandler: self }, self.onAddToCartForm );
26
27		if(typeof wNumb == 'function' && typeof qty_summ_args != 'undefined')
28		{
29			self.summ_format = wNumb({
30				mark: qty_summ_args.format.mark,
31				thousand: qty_summ_args.format.thousand,
32				decimals: qty_summ_args.format.decimals,
33				suffix: qty_summ_args.format.suffix,
34				prefix: qty_summ_args.format.prefix,
35			});
36			if(qty_summ_args.mode == 'variable')
37			{
38				$( document.body )
39					.on('reload_product_variations show_variation', 'form.variations_form', function(event,variation){
40						self.summ_pid = variation['variation_id'];
41						$form = $(this);
42						$form.find('input[name="quantity"]').trigger({
43							type:"change",
44							Add2CartHandler: self,
45							mode: 0,
46						});
47					})
48					.on('hide_variation', 'form.variations_form', function(event){
49						self.summ_pid = false;
50						$form = $(this);
51						$form.find('input[name="quantity"]').trigger({
52							type:"change",
53							Add2CartHandler: self,
54							mode: 0,
55						});
56					});
57			}
58		}
59
60		return this;
61	};
62
63	/**
64	 * Add add to favorites event.
65	 */
66	Add2CartHandler.prototype.addRequest = function( request ) {
67		this.requests.push( request );
68
69		if ( 1 === this.requests.length ) {
70			this.run();
71		}
72	};
73
74	/**
75	 * Run add to favorites events.
76	 */
77	Add2CartHandler.prototype.run = function() {
78		var requestManager = this,
79			originalCallback = requestManager.requests[0].complete;
80
81		requestManager.requests[0].complete = function() {
82			if ( typeof originalCallback === 'function' ) {
83				originalCallback();
84			}
85
86			requestManager.requests.shift();
87
88			if ( requestManager.requests.length > 0 ) {
89				requestManager.run();
90			}
91		};
92
93		$.ajax( this.requests[0] );
94	};
95
96	/**
97	 * Handle the add to favorites event.
98	 */
99	Add2CartHandler.prototype.onAddToCartForm = function( e ) {
100
101		e.preventDefault();
102
103		var $form = $(this);
104
105		$form.block({
106			message: null,
107			overlayCSS: {
108				backgroundColor: '#fff',
109				opacity: 0.6
110			}
111		});
112
113		var formData = new FormData(this);
114
115		$form.find('[type="submit"]').each(function(index, el) {
116		  var $el;
117		  $el = $(el);
118		  formData.append($el.attr('name'), $el.val());
119		  return true;
120		});
121
122		var data = {};
123
124		formData.forEach(function(value, key){
125			data[key] = value;
126		});
127
128		if( ('add-to-cart' in data) && !('product_id' in data))
129		{
130			data['product_id'] = data['add-to-cart'];
131		}
132		delete data['add-to-cart'];
133
134		$( document.body ).trigger( 'adding_to_cart', [ $form, data ] );
135
136		e.data.Add2CartHandler.addRequest({
137			type: 'POST',
138			url: wc_add_to_cart_params.wc_ajax_url.toString().replace('%%endpoint%%', 'add2cart'),
139			data: data,
140			// processData: false,
141			// contentType: false,
142			// dataType: 'json',
143			success: function(response) {
144				if (!response) {
145				  return;
146				}
147
148				if (response.error && response.product_url) {
149				  window.location = response.product_url;
150				  return;
151				}
152
153				if (wc_add_to_cart_params.cart_redirect_after_add === 'yes') {
154				  window.location = wc_add_to_cart_params.cart_url;
155				  return;
156				}
157
158				$thisbutton = $(document.body).find('.ajax_add_to_cart[data-
158product_id="'+formData.get('add-to-cart')+'"]');
159
160				$( document.body ).trigger( 'added_to_cart_proxy', [ response, $thisbutton ] );
161
162				$form.unblock();
163				return;
164			}
165		});
166
167	};
168
169	/**
170	 * Handle the add to cart event.
171	 */
172	Add2CartHandler.prototype.onAddToCart = function( e ) {
173
174		var $thisbutton = $( this );
175
176		if ( $thisbutton.is( '.ajax_add_to_cart' ) ) {
177			if ( ! $thisbutton.attr( 'data-product_id' ) ) {
178				return true;
179			}
180
181			e.preventDefault();
182
183			$thisbutton.removeClass( 'added' );
184			$thisbutton.addClass( 'loading' );
185
186			$thisbutton.block({
187				message: null,
188				overlayCSS: {
189					backgroundColor: '#fff',
190					opacity: 0.6
191				}
192			});
193
194			// Allow 3rd parties to validate and quit early.
195			if ( false === $( document.body ).triggerHandler( 'should_send_ajax_request.adding_to_cart', [ $thisbutton ] ) ) { 
196				$( document.body ).trigger( 'ajax_request_not_sent.adding_to_cart', [ false, false, $thisbutton ] );
197				return true;
198			}
199
200			var data = {};
201
202			// Fetch changes that are directly added by calling $thisbutton.data( key, value )
203			$.each( $thisbutton.data(), function( key, value ) {
204				data[ key ] = value;
205			});
206
207			// Fetch data attributes in $thisbutton. Give preference to data-attributes because they can be directly modified by javascript
208			// while `.data` are jquery specific memory stores.
209			$.each( $thisbutton[0].dataset, function( key, value ) {
210				data[ key ] = value;
211			});
212
213			// Trigger event.
214			$( document.body ).trigger( 'adding_to_cart', [ $thisbutton, data ] );
215
216			e.data.Add2CartHandler.addRequest({
217				type: 'POST',
218				url: wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'add2cart' ),
219				data: data,
220				success: function( response ) {
221					if ( ! response ) {
222						return;
223					}
224
225					if ( response.error && response.product_url ) {
226						window.location = response.product_url;
227						return;
228					}
229
230					// Redirect to cart option
231					if ( wc_add_to_cart_params.cart_redirect_after_add === 'yes' ) {
232						window.location = wc_add_to_cart_params.cart_url;
233						return;
234					}
235
236					// Trigger event so themes can refresh other areas.
237					$( document.body ).trigger( 'added_to_cart_proxy', [ response, $thisbutton ] );
238				},
239				dataType: 'json'
240			});
241		}
242	};
243
244	/**
245	 * Update fragments after remove from cart event in mini-cart.
246	 */
247	Add2CartHandler.prototype.onRemoveFromCart = function( e ) {
248		var $thisbutton = $( this ),
249			$row        = $thisbutton.closest( '.woocommerce-mini-cart-item' );
250
251		e.preventDefault();
252
253		$row.block({
254			message: null,
255			overlayCSS: {
256				opacity: 0.6
257			}
258		});
259
260		e.data.Add2CartHandler.addRequest({
261			type: 'POST',
262			url: wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'remove_from_cart' ),
263			data: {
264				cart_item_key : $thisbutton.data( 'cart_item_key' ),
265				include_cart_notice_fragment : true,
266			},
267			success: function( response ) {
268				if ( ! response || ! response.fragments ) {
269					window.location = $thisbutton.attr( 'href' );
270					return;
271				}
272
273				$( document.body ).trigger( 'removed_from_cart_proxy', [ response, $thisbutton ] );
274			},
275			error: function() {
276				window.location = $thisbutton.attr( 'href' );
277				return;
278			},
279			dataType: 'json'
280		});
281	};
282
283	/**
284	 * Update cart page elements after add to cart events.
285	 */
286	Add2CartHandler.prototype.updateButton = function( e, fragments, cart_hash, $button ) {
287		$button = typeof $button === 'undefined' ? false : $button;
288
289		if ( $button ) {
290			$button.removeClass( 'loading' );
291			
292			if ( fragments ) {
293				$button.addClass( 'added' );
294			}
295
296			$( document.body ).trigger( 'wc_cart_button_updated', [ $button ] );
297		}
298	};
299
300	/**
301	 * Update fragments after add to cart events.
302	 */
303	Add2CartHandler.prototype.updateFragments = function( e, response, $thisbutton ) {
304
305		var self = e.data['Add2CartHandler'];
306
307		var fragments = response.fragments;
308		var cart_hash = response.cart_hash;
309
310		if ( fragments ) {
311
312			var alerts = {}
313			if('.woocommerce-notices-wrapper' in fragments)
314			{
315				alerts['.woocommerce-notices-wrapper'] = fragments['.woocommerce-notices-wrapper'];
316			}
317			if('div.woocommerce-notices-wrapper' in fragments)
318			{
319				alerts['div.woocommerce-notices-wrapper'] = fragments['div.woocommerce-notices-wrapper'];
320			}
321			delete fragments['.woocommerce-notices-wrapper'];
322			delete fragments['div.woocommerce-notices-wrapper'];
323
324			if(alerts)
325			{
326				$.each( alerts, function( key, value ) {
327					$( key ).append( value );
328					$( key ).stop( true ).css( 'opacity', '1' );
329				});
330				$( document.body ).trigger( 'notifications_loaded' );
331			}
332
333			$.each( fragments, function( key, value ) {
334				$( key ).replaceWith( value );
335				$( key ).stop( true ).css( 'opacity', '1' ).unblock();
336			});
337
338			$thisbutton.unblock();
339
340			if('modal' in response)
341			{
342				self.makeModal(response.modal);
343			}
344
345			if(e.type == 'added_to_cart_proxy')
346			{
347				$( document.body ).trigger( 'added_to_cart', [ fragments, cart_hash, $thisbutton ] );
348			}
349			if(e.type == 'removed_from_cart_proxy')
350			{
351				$( document.body ).trigger( 'removed_from_cart', [ fragments, cart_hash, $thisbutton ] );
352			}
353
354			$( document.body ).trigger( 'wc_fragments_loaded' );
355		}
356	};
357
358	Add2CartHandler.prototype.makeModal = function( content ) {
359		var config, modal;
360		config = {
361			title: 'Товар добавлен в корзину',
362			cclass: 'added-to-cart-modal',
363			content: content,
364			buttons: [],
365			timeout: 300,
366			autoopen: false,
367			close_on_bg: true,
368			scrollinside: true,
369		};
370		modal = new Modal(config);
371		modal.open(self.modal?0:false)
372		if(self.modal)
373		{
374			self.modal.close(0);
375			$(document.body).off('closeModal.addToCart');
376			self.modal=null;
377		}
378		$(document.body).on('closeModal.addToCart',function(e,p){
379			$(document.body).off('closeModal.addToCart');
380			self.modal=null;
381		});
382		self.modal=modal;
383	};
384
385	/**
386	 * Init Add2CartHandler.
387	 */
388	new Add2CartHandler();
389});

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.