-
Notifications
You must be signed in to change notification settings - Fork 12
/
store product detail add to cart text change on quantity change.html
276 lines (141 loc) · 6.96 KB
/
store product detail add to cart text change on quantity change.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<script>
/*
begin store product detail add to cart text change on quantity change
License : < https://tinyurl.com/s872fb68 >
Version : 0.1.0
SS Versions : 7.1, 7.0
Dependancies : twcsl
Note : changing the add to cart label does not effect the cart,
checkout, or other SS pages/backend storage. this is purely
a cosmetic change
By : Thomas Creedon < http://www.tomsWeb.consulting/ >
*/
$( ( ) => {
/* text of add to cart button label to replace, replaces the first
occurrence of text in label */
const searchText = ' ';
/*
replacement text, text that replaces the search text in label, use [n]
in text to insert the calculated quantity value. following are some
example values :
const replaceText = ' [q] Ounces ';
const replaceText = ' [q] Square Feet ';
const replaceText = ' [q] Yards ';
*/
const replaceText = '[enter replacement text here between single quotes replacing square brackets]';
const multiplier = 1; // value to multiply quantity by
const displayAsFraction = false; // use false or true
// do not change anything below, there be the borg here
// bail if no mutation observer available
if ( ! ( 'MutationObserver' in window ) ) return;
if ( ! twcsl.page.store.detail.is ) return; // bail if not detail
const initialize = ( ) => {
const addToCartInnerObserve = ( ) => {
// listen for changes in elements
$( selector ).each ( function ( ) {
observer.observe ( this, {
attributes : true,
attributeFilter : [ attribute ],
attributeOldValue : true,
characterData : true,
childList : true
} );
} );
};
const attribute = 'data-twc-spdatctcoqc-mo';
const label = $( '.sqs-add-to-cart-button' )
.first ( )
.attr ( 'data-original-label' );
const observer = new MutationObserver ( mutations => {
$.each ( mutations, function ( ) {
// console.log ( this );
// previous change was forced
const b = this.type == 'attributes' &&
this.attributeName == attribute;
let node;
if ( b ) {
const $this = $( this.target );
if ( $this.attr ( attribute ) == 'true' ) {
const quantity = $this
.parents ( '.sqs-add-to-cart-button-wrapper' )
.prev ( '.product-quantity-input' )
.find ( 'input' )
.val ( );
observer.disconnect ( );
node = $this.contents ( ).get ( 0 );
setLabel ( node, quantity )
$this.attr ( attribute, 'false' );
addToCartInnerObserve ( );
} else if ( this.oldValue == 'true' )
return;
}
if ( ! this.addedNodes.length ) return;
node = this.addedNodes [ 0 ];
if ( node.nodeName != '#text' ) return; // bail if not text node
if ( node.data != label ) return; // bail if not label
const quantity = $( node )
.parents ( '.sqs-add-to-cart-button-wrapper' )
.prev ( '.product-quantity-input' )
.find ( 'input' )
.val ( );
observer.disconnect ( );
setLabel ( node, quantity )
addToCartInnerObserve ( );
} );
} );
const selector = '.sqs-add-to-cart-button-wrapper ' +
'.sqs-add-to-cart-button-inner';
const setLabel = ( node, quantity = 1 ) => {
const getLowestFraction = ( x0 ) => {
// based on code by Martin R < https://bit.ly/3yVE15b >
const eps = 1.0E-15;
let h2;
let k2;
let x = x0;
let a = h = Math.floor ( x );
let h1 = k = 1;
let k1 = 0;
while ( x - a > eps * k * k ) {
x = 1 / ( x - a );
a = Math.floor ( x );
h2 = h1;
h1 = h;
k2 = k1;
k1 = k;
h = h2 + a * h1;
k = k2 + a * k1;
}
return h + '/' + k;
};
quantity = quantity * multiplier;
const isInteger = Number.isInteger ( quantity );
if ( displayAsFraction && ! isInteger ) {
var [ w, f ] = quantity
.toString ( )
.split ( '.' );
if ( w == '0' ) w = '';
f = Number ( `0.${ f }` );
f = getLowestFraction ( f )
quantity = `${ w } ${ f }`;
}
const r = replaceText.replace ( '[q]', quantity );
const text = label.replace ( searchText, r );
node.data = text;
};
addToCartInnerObserve ( );
$( '.product-quantity-input' ).change ( function ( ) {
$( selector ).attr ( attribute, 'true' );
} );
$( selector ).attr ( attribute, 'true' );
};
switch ( true ) {
case twcsl.ss.is71 :
$( initialize );
break;
case twcsl.ss.is70 :
Squarespace.onInitialize ( Y, initialize );
break;
}
} );
// end store product detail add to cart text change on quantity change
</script>