PageSourceSearch

https://daan.tech/wp-content/themes/shoptimizer/assets/js/minicart-quantity.js?ver=1791073370

js daan.tech collected 2026-10-04 00:24:42 UTC 3,106 bytes, 110 lines download raw bytes

1/**
2 *
3 * Shoptimizer quantity custom script
4 *
5 * @package Shoptimizer
6 */
7
8let changEv   = new Event( 'change' );
9let sidebarEl = [];
10let timerId;
11let miniItems;
12const shopWidget = document.querySelector( '.widget_shopping_cart' );
13
14const shoptimizerDelayFunction = function( func, delay ) {
15	clearTimeout( timerId );
16	timerId = setTimeout( func, delay );
17};
18function shoptimizerInitMiniCartQty() {
19	if ( ! shopWidget ) {
20		return;
21	}
22	sidebarEl = [];
23	miniItems = shopWidget.querySelectorAll( '.shoptimizer-custom-quantity-mini-cart' );
24	miniItems.forEach(
25		function( item ) {
26			sidebarEl.push(
27				{
28					qtInput: item.querySelector( 'input' ),
29					qtButtons: Array.from( item.querySelectorAll( '.shoptimizer-custom-quantity-mini-cart_button' ) )
30				}
31			);
32		}
33	);
34	sidebarEl.forEach(
35		function( item ) {
36			item.qtButtons.forEach(
37				function( btn ) {
38					btn.addEventListener( 'click', shoptimizerEachSideBtnListener );
39				}
40			);
41			item.qtInput.addEventListener( 'change', shoptimizerUpdateMiniCart );
42		}
43	);
44}
45function shoptimizerEachSideBtnListener() {
46	const item   = sidebarEl.find( ( el ) => el.qtButtons.includes( this ) );
47	let value    = parseInt( item.qtInput.value, 10 );
48	value        = isNaN( value ) ? 1 : value;
49	let minValue = parseInt( item.qtInput.getAttribute( 'min' ), 10 );
50	let maxValue = parseInt( item.qtInput.getAttribute( 'max' ), 10 );
51	if ( this.classList.contains( 'quantity-up' ) ) {
52		value++;
53		if ( ! isNaN( maxValue ) && value > maxValue ) {
54			value = maxValue;
55		}
56	} else {
57		value--;
58		if ( ! isNaN( minValue ) && value < minValue ) {
59			value = minValue;
60		}
61		1 > value ? ( value = 1 ) : '';
62	}
63	if ( item.qtInput.value != value ) {
64		item.qtInput.value = value;
65		item.qtInput.dispatchEvent( changEv );
66	}
67}
68function shoptimizerUpdateMiniCart() {
69	shoptimizerDelayFunction(
70		function() {
71			var formData = new FormData();
72			miniItems    = document.querySelectorAll( '.shoptimizer-custom-quantity-mini-cart' );
73			miniItems.forEach(
74				function( item ) {
75					let input   = item.querySelector( 'input' );
76					let qty     = parseInt( input.value, 10 );
77					qty         = isNaN( qty ) ? 1 : qty;
78					let itemKey = input.getAttribute( 'data-cart_item_key' );
79					formData.append( 'data[' + itemKey + ']', qty );
80				}
81			);
82			formData.append( 'action', 'cg_shoptimizer_update_mini_cart' );
83			var ajax_loader = document.querySelector( '#ajax-loading' );
84			if ( ajax_loader ) {
85				ajax_loader.style.display = 'block';
86			}
87			fetch(
88				woocommerce_params.ajax_url,
89				{
90					method: 'POST',
91					body: formData
92				}
93			).then(
94				response => response.json()
95			).then(
96				function( json ) {
97					var wcfragment = new Event( 'wc_fragment_refresh' );
98					document.body.dispatchEvent( wcfragment );
99					var wccart = document.querySelectorAll( 'form.woocommerce-cart-form input.qty, form.woocommerce-checkout' );
100					if ( 0 < wccart.length ) {
101						window.location.reload();
102					}
103				}
104			);
105		},
106		600
107	);
108}
109jQuery( 'body' ).on( 'wc_fragments_refreshed wc_fragments_loaded', shoptimizerInitMiniCartQty );
110shoptimizerInitMiniCartQty();

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.