PageSourceSearch

https://www.routinewellness.com/wp-content/plugins/woocommerce/ass…/frontend/add-to-cart-variation.js?ver=1.2

js routinewellness.com collected 2026-10-09 21:53:18 UTC 18,658 bytes, 538 lines download raw bytes

1/*global wc_add_to_cart_variation_params, wc_cart_fragments_params */
2/*!
3 * Variations Plugin
4 */
5;(function ( $, window, document, undefined ) {
6
7	$.fn.wc_variation_form = function() {
8		var $form                  = this,
9			$single_variation      = $form.find( '.single_variation' ),
10			$product               = $form.closest( '.product' ),
11			$product_id            = parseInt( $form.data( 'product_id' ), 10 ),
12			$product_variations    = $form.data( 'product_variations' ),
13			$use_ajax              = $product_variations === false,
14			$xhr                   = false,
15			$reset_variations      = $form.find( '.reset_variations' ),
16			template               = wp.template( 'variation-template' ),
17			unavailable_template   = wp.template( 'unavailable-variation-template' ),
18			$single_variation_wrap = $form.find( '.single_variation_wrap' );
19
20		// Always visible since 2.5.0
21		$single_variation_wrap.show();
22
23		// Unbind any existing events
24		$form.unbind( 'check_variations update_variation_values found_variation' );
25		$form.find( '.reset_variations' ).unbind( 'click' );
26		$form.find( '.variations select' ).unbind( 'change focusin' );
27
28		// Bind new events to form
29		$form
30
31		// On clicking the reset variation button
32		.on( 'click', '.reset_variations', function( event ) {
33			event.preventDefault();
34			$form.find( '.variations select' ).val( '' ).change();
35			$form.trigger( 'reset_data' );
36		} )
37
38		// When the variation is hidden
39		.on( 'hide_variation', function( event ) {
40			event.preventDefault();
41			$form.find( '.single_add_to_cart_button' ).removeClass( 'wc-variation-is-unavailable' ).addClass( 'disabled wc-variation-selection-needed' );
42			$form.find( '.woocommerce-variation-add-to-cart' ).removeClass( 'woocommerce-variation-add-to-cart-enabled' ).addClass( 'woocommerce-variation-add-to-cart-disabled' );
43		} )
44
45		// When the variation is revealed
46		.on( 'show_variation', function( event, variation, purchasable ) {
47			event.preventDefault();
48			if ( purchasable ) {
49				$form.find( '.single_add_to_cart_button' ).removeClass( 'disabled wc-variation-selection-needed wc-variation-is-unavailable' );
50				$form.find( '.woocommerce-variation-add-to-cart' ).removeClass( 'woocommerce-variation-add-to-cart-disabled' ).addClass( 'woocommerce-variation-add-to-cart-enabled' );
51			} else {
52				$form.find( '.single_add_to_cart_button' ).removeClass( 'wc-variation-selection-needed' ).addClass( 'disabled wc-variation-is-unavailable' );
53				$form.find( '.woocommerce-variation-add-to-cart' ).removeClass( 'woocommerce-variation-add-to-cart-enabled' ).addClass( 'woocommerce-variation-add-to-cart-disabled' );
54			}
55		} )
56
57		.on( 'click', '.single_add_to_cart_button', function( event ) {
58			var $this = $( this );
59
60			if ( $this.is('.disabled') ) {
61				event.preventDefault();
62
63				if ( $this.is('.wc-variation-is-unavailable') ) {
64					window.alert( wc_add_to_cart_variation_params.i18n_unavailable_text );
65				} else if ( $this.is('.wc-variation-selection-needed') ) {
66					window.alert( wc_add_to_cart_variation_params.i18n_make_a_selection_text );
67				}
68			}
69		} )
70
71		// Reload product variations data
72		.on( 'reload_product_variations', function() {
73			$product_variations = $form.data( 'product_variations' );
74			$use_ajax           = $product_variations === false;
75		} )
76
77		// Reset product data
78		.on( 'reset_data', function() {
79			$product.find( '.product_meta' ).find( '.sku' ).wc_reset_content();
80			$('.product_weight').wc_reset_content();
81			$('.product_dimensions').wc_reset_content();
82			$form.trigger( 'reset_image' );
83			$single_variation.slideUp( 200 ).trigger( 'hide_variation' );
84		} )
85
86		// Reset product image
87		.on( 'reset_image', function() {
88			$form.wc_variations_image_update( false );
89		} )
90
91		// On changing an attribute
92		.on( 'change', '.variations select', function() {
93			$form.find( 'input[name="variation_id"], input.variation_id' ).val( '' ).change();
94			$form.find( '.wc-no-matching-variations' ).remove();
95
96			if ( $use_ajax ) {
97				if ( $xhr ) {
98					$xhr.abort();
99				}
100
101				var all_attributes_chosen  = true;
102				var some_attributes_chosen = false;
103				var data                   = {};
104
105				$form.find( '.variations select' ).each( function() {
106					var attribute_name = $( this ).data( 'attribute_name' ) || $( this ).attr( 'name' );
107
108					if ( $( this ).val().length === 0 ) {
109						all_attributes_chosen = false;
110					} else {
111						some_attributes_chosen = true;
112					}
113
114					data[ attribute_name ] = $( this ).val();
115				});
116
117				if ( all_attributes_chosen ) {
118					// Get a matchihng variation via ajax
119					data.product_id = $product_id;
120
121					$( '.variations_form' ).block({
122						message: null,
123						overlayCSS: {
124							background: '#fff',
125							opacity: 0.6
126						}
127					});
128
129					$xhr = $.ajax( {
130						url: wc_cart_fragments_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'get_variation' ),
131						type: 'POST',
132						data: data,
133						success: function( variation ) {
134							if ( variation ) {
135								$form.trigger( 'found_variation', [ variation ] );
136							} else {
137								$form.trigger( 'reset_data' );
138								$form.find( '.single_variation' ).after( '<p class="wc-no-matching-variations woocommerce-info">' + wc_add_to_cart_variation_params.i18n_no_matching_variations_text + '</p>' );
139								$form.find( '.wc-no-matching-variations' ).slideDown( 200 );
140							}
141						},
142						complete: function() {
143							$( '.variations_form' ).unblock();
144						}
145					} );
146				} else {
147					$form.trigger( 'reset_data' );
148				}
149				if ( some_attributes_chosen ) {
150					if ( $reset_variations.css( 'visibility' ) === 'hidden' ) {
151						$reset_variations.css( 'visibility', 'visible' ).hide().fadeIn();
152					}
153				} else {
154					$reset_variations.css( 'visibility', 'hidden' );
155				}
156			} else {
157				$form.trigger( 'woocommerce_variation_select_change' );
158				$form.trigger( 'check_variations', [ '', false ] );
159				$( this ).blur();
160			}
161
162			// added to get around variation image flicker issue
163			$( '.product.has-default-attributes > .images' ).fadeTo( 200, 1 );
164
165			// Custom event for when variation selection has been changed
166			$form.trigger( 'woocommerce_variation_has_changed' );
167		} )
168
169		// Upon gaining focus
170		.on( 'focusin touchstart', '.variations select', function() {
171			$( this ).find( 'option:selected' ).attr( 'selected', 'selected' );
172
173			if ( ! $use_ajax ) {
174				$form.trigger( 'woocommerce_variation_select_focusin' );
175				$form.trigger( 'check_variations', [ $( this ).data( 'attribute_name' ) || $( this ).attr( 'name' ), true ] );
176			}
177		} )
178
179		// Show single variation details (price, stock, image)
180		.on( 'found_variation', function( event, variation ) {
181			var $sku        = $product.find( '.product_meta' ).find( '.sku' ),
182				$weight     = $product.find( '.product_weight' ),
183				$dimensions = $product.find( '.product_dimensions' ),
184				$qty        = $single_variation_wrap.find( '.quantity' ),
185				purchasable = true;
186
187			// Display SKU
188			if ( variation.sku ) {
189				$sku.wc_set_content( variation.sku );
190			} else {
191				$sku.wc_reset_content();
192			}
193
194			// Display weight
195			if ( variation.weight ) {
196				$weight.wc_set_content( variation.weight );
197			} else {
198				$weight.wc_reset_content();
199			}
200
201			// Display dimensions
202			if ( variation.dimensions ) {
203				$dimensions.wc_set_content( variation.dimensions );
204			} else {
205				$dimensions.wc_reset_content();
206			}
207
208			// Show images
209			$form.wc_variations_image_update( variation );
210
211			// Output correct templates
212			var $template_html = '';
213
214			if ( ! variation.variation_is_visible ) {
215				$template_html = unavailable_template();
216				// w3 total cache inline minification adds CDATA tags around our HTML (sigh)
217				$template_html = $template_html.replace( '/*<![CDATA[*/', '' );
218				$template_html = $template_html.replace( '/*]]>*/', '' );
219				$single_variation.html( $template_html );
220				$form.find( 'input[name="variation_id"], input.variation_id' ).val( '' ).change();
221			} else {
222				$template_html = template( {
223					variation:    variation
224				} );
225				// w3 total cache inline minification adds CDATA tags around our HTML (sigh)
226				$template_html = $template_html.replace( '/*<![CDATA[*/', '' );
227				$template_html = $template_html.replace( '/*]]>*/', '' );
228				$single_variation.html( $template_html );
229				$form.find( 'input[name="variation_id"], input.variation_id' ).val( variation.variation_id ).change();
230			}
231
232			// Hide or show qty input
233			if ( variation.is_sold_individually === 'yes' ) {
234				$qty.find( 'input.qty' ).val( '1' ).attr( 'min', '1' ).attr( 'max', '' );
235				$qty.hide();
236			} else {
237				$qty.find( 'input.qty' ).attr( 'min', variation.min_qty ).attr( 'max', variation.max_qty );
238				$qty.show();
239			}
240
241			// Enable or disable the add to cart button
242			if ( ! variation.is_purchasable || ! variation.is_in_stock || ! variation.variation_is_visible ) {
243				purchasable = false;
244			}
245
246			// Reveal
247			if ( $.trim( $single_variation.text() ) ) {
248				$single_variation.slideDown( 200 ).trigger( 'show_variation', [ variation, purchasable ] );
249			} else {
250				$single_variation.show().trigger( 'show_variation', [ variation, purchasable ] );
251			}
252		})
253
254		// Check variations
255		.on( 'check_variations', function( event, exclude, focus ) {
256			if ( $use_ajax ) {
257				return;
258			}
259
260			var all_attributes_chosen  = true,
261				some_attributes_chosen = false,
262				current_settings       = {},
263				$form                  = $( this ),
264				$reset_variations      = $form.find( '.reset_variations' );
265
266			$form.find( '.variations select' ).each( function() {
267				var attribute_name = $( this ).data( 'attribute_name' ) || $( this ).attr( 'name' );
268
269				if ( $( this ).val().length === 0 ) {
270					all_attributes_chosen = false;
271				} else {
272					some_attributes_chosen = true;
273				}
274
275				if ( exclude && attribute_name === exclude ) {
276					all_attributes_chosen = false;
277					current_settings[ attribute_name ] = '';
278				} else {
279					// Add to settings array
280					current_settings[ attribute_name ] = $( this ).val();
281				}
282			});
283
284			var matching_variations = wc_variation_form_matcher.find_matching_variations( $product_variations, current_settings );
285
286			if ( all_attributes_chosen ) {
287
288				var variation = matching_variations.shift();
289
290				if ( variation ) {
291					$form.trigger( 'found_variation', [ variation ] );
292				} else {
293					// Nothing found - reset fields
294					$form.find( '.variations select' ).val( '' );
295
296					if ( ! focus ) {
297						$form.trigger( 'reset_data' );
298					}
299
300					window.alert( wc_add_to_cart_variation_params.i18n_no_matching_variations_text );
301				}
302
303			} else {
304
305				$form.trigger( 'update_variation_values', [ matching_variations ] );
306
307				if ( ! focus ) {
308					$form.trigger( 'reset_data' );
309				}
310
311				if ( ! exclude ) {
312					$single_variation.slideUp( 200 ).trigger( 'hide_variation' );
313				}
314			}
315			if ( some_attributes_chosen ) {
316				if ( $reset_variations.css( 'visibility' ) === 'hidden' ) {
317					$reset_variations.css( 'visibility', 'visible' ).hide().fadeIn();
318				}
319			} else {
320				$reset_variations.css( 'visibility', 'hidden' );
321			}
322		} )
323
324		// Disable option fields that are unavaiable for current set of attributes
325		.on( 'update_variation_values', function( event, variations ) {
326			if ( $use_ajax ) {
327				return;
328			}
329			// Loop through selects and disable/enable options based on selections
330			$form.find( '.variations select' ).each( function( index, el ) {
331
332				var current_attr_name, current_attr_select = $( el );
333
334				// Reset options
335				if ( ! current_attr_select.data( 'attribute_options' ) ) {
336					current_attr_select.data( 'attribute_options', current_attr_select.find( 'option:gt(0)' ).get() );
337				}
338
339				current_attr_select.find( 'option:gt(0)' ).remove();
340				current_attr_select.append( current_attr_select.data( 'attribute_options' ) );
341				current_attr_select.find( 'option:gt(0)' ).removeClass( 'attached' );
342				current_attr_select.find( 'option:gt(0)' ).removeClass( 'enabled' );
343				current_attr_select.find( 'option:gt(0)' ).removeAttr( 'disabled' );
344
345				// Get name from data-attribute_name, or from input name if it doesn't exist
346				if ( typeof( current_attr_select.data( 'attribute_name' ) ) !== 'undefined' ) {
347					current_attr_name = current_attr_select.data( 'attribute_name' );
348				} else {
349					current_attr_name = current_attr_select.attr( 'name' );
350				}
351
352				// Loop through variations
353				for ( var num in variations ) {
354
355					if ( typeof( variations[ num ] ) !== 'undefined' ) {
356
357						var attributes = variations[ num ].attributes;
358
359						for ( var attr_name in attributes ) {
360							if ( attributes.hasOwnProperty( attr_name ) ) {
361								var attr_val = attributes[ attr_name ];
362
363								if ( attr_name === current_attr_name ) {
364
365									var variation_active = '';
366
367									if ( variations[ num ].variation_is_active ) {
368										variation_active = 'enabled';
369									}
370
371									if ( attr_val ) {
372
373										// Decode entities
374										attr_val = $( '<div/>' ).html( attr_val ).text();
375
376										// Add slashes
377										attr_val = attr_val.replace( /'/g, '\\\'' );
378										attr_val = attr_val.replace( /"/g, '\\\"' );
379
380										// Compare the meerkat
381										current_attr_select.find( 'option[value="' + attr_val + '"]' ).addClass( 'attached ' + variation_active );
382
383									} else {
384
385										current_attr_select.find( 'option:gt(0)' ).addClass( 'attached ' + variation_active );
386
387									}
388								}
389							}
390						}
391					}
392				}
393
394				// Detach unattached
395				current_attr_select.find( 'option:gt(0):not(.attached)' ).remove();
396
397				// Grey out disabled
398				current_attr_select.find( 'option:gt(0):not(.enabled)' ).attr( 'disabled', 'disabled' );
399
400			});
401
402			// Custom event for when variations have been updated
403			$form.trigger( 'woocommerce_update_variation_values' );
404		});
405
406		$form.trigger( 'wc_variation_form' );
407
408		return $form;
409	};
410
411	/**
412	 * Matches inline variation objects to chosen attributes
413	 * @type {Object}
414	 */
415	var wc_variation_form_matcher = {
416		find_matching_variations: function( product_variations, settings ) {
417			var matching = [];
418			for ( var i = 0; i < product_variations.length; i++ ) {
419				var variation    = product_variations[i];
420
421				if ( wc_variation_form_matcher.variations_match( variation.attributes, settings ) ) {
422					matching.push( variation );
423				}
424			}
425			return matching;
426		},
427		variations_match: function( attrs1, attrs2 ) {
428			var match = true;
429			for ( var attr_name in attrs1 ) {
430				if ( attrs1.hasOwnProperty( attr_name ) ) {
431					var val1 = attrs1[ attr_name ];
432					var val2 = attrs2[ attr_name ];
433					if ( val1 !== undefined && val2 !== undefined && val1.length !== 0 && val2.length !== 0 && val1 !== val2 ) {
434						match = false;
435					}
436				}
437			}
438			return match;
439		}
440	};
441
442	/**
443	 * Stores the default text for an element so it can be reset later
444	 */
445	$.fn.wc_set_content = function( content ) {
446		if ( undefined === this.attr( 'data-o_content' ) ) {
447			this.attr( 'data-o_content', this.text() );
448		}
449		this.text( content );
450	};
451
452	/**
453	 * Stores the default text for an element so it can be reset later
454	 */
455	$.fn.wc_reset_content = function() {
456		if ( undefined !== this.attr( 'data-o_content' ) ) {
457			this.text( this.attr( 'data-o_content' ) );
458		}
459	};
460
461	/**
462	 * Stores a default attribute for an element so it can be reset later
463	 */
464	$.fn.wc_set_variation_attr = function( attr, value ) {
465		if ( undefined === this.attr( 'data-o_' + attr ) ) {
466			this.attr( 'data-o_' + attr, ( ! this.attr( attr ) ) ? '' : this.attr( attr ) );
467		}
468		this.attr( attr, value );
469	};
470
471	/**
472	 * Reset a default attribute for an element so it can be reset later
473	 */
474	$.fn.wc_reset_variation_attr = function( attr ) {
475		if ( undefined !== this.attr( 'data-o_' + attr ) ) {
476			this.attr( attr, this.attr( 'data-o_' + attr ) );
477		}
478	};
479
480	/**
481	 * Sets product images for the chosen variation
482	 */
483	$.fn.wc_variations_image_update = function( variation ) {
484		var $form             = this,
485			$product          = $form.closest('.product'),
486			$product_img      = $product.find( 'div.images img:eq(0)' ),
487			$product_link     = $product.find( 'div.images a.zoom:eq(0)' );
488
489		if ( variation && variation.image_src && variation.image_src.length > 1 ) {
490
491                    if ( variation.sku == "3124 - Single - Coconut" && window.__optimizelyCoconutImageTest){
492
493                        var testVariationInfo = {
494                            "image_src": "https:\/\/www.nativecos.com\/wp-content\/uploads\/2017\/05\/Womens-Health.jpg",
495                            "image_title": "Womens-Health",
496                            "image_srcset": "",
497                            "image_sizes": "(max-width: 620px) 100vw, 620px",
498                            "image_link": "https:\/\/www.nativecos.com\/wp-content\/uploads\/2017\/05\/Womens-Health.jpg",
499                            "image_alt": "Womens-Health",
500                            "image_caption": ""
501                        };
502
503			$product_img.wc_set_variation_attr( 'src', testVariationInfo.image_src );
504			$product_img.wc_set_variation_attr( 'title', testVariationInfo.image_title );
505			$product_img.wc_set_variation_attr( 'alt', testVariationInfo.image_alt );
506			$product_img.wc_set_variation_attr( 'srcset', testVariationInfo.image_srcset );
507			$product_img.wc_set_variation_attr( 'sizes', testVariationInfo.image_sizes );
508			$product_link.wc_set_variation_attr( 'href', testVariationInfo.image_link );
509			$product_link.wc_set_variation_attr( 'title', testVariationInfo.image_caption );
510                    }else{
511			$product_img.wc_set_variation_attr( 'src', variation.image_src );
512			$product_img.wc_set_variation_attr( 'title', variation.image_title );
513			$product_img.wc_set_variation_attr( 'alt', variation.image_alt );
514			$product_img.wc_set_variation_attr( 'srcset', variation.image_srcset );
515			$product_img.wc_set_variation_attr( 'sizes', variation.image_sizes );
516			$product_link.wc_set_variation_attr( 'href', variation.image_link );
517			$product_link.wc_set_variation_attr( 'title', variation.image_caption );
518                    }
519		} else {
520			$product_img.wc_reset_variation_attr( 'src' );
521			$product_img.wc_reset_variation_attr( 'title' );
522			$product_img.wc_reset_variation_attr( 'alt' );
523			$product_img.wc_reset_variation_attr( 'srcset' );
524			$product_img.wc_reset_variation_attr( 'sizes' );
525			$product_link.wc_reset_variation_attr( 'href' );
526			$product_link.wc_reset_variation_attr( 'title' );
527		}
528	};
529
530	$( function() {
531		if ( typeof wc_add_to_cart_variation_params !== 'undefined' ) {
532			$( '.variations_form' ).each( function() {
533				$( this ).wc_variation_form().find('.variations select:eq(0)').change();
534			});
535		}
536	});
537
538})( jQuery, window, document );

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.