PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 3.3.0
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v3.3.0
3.3.0 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.1 trunk 1.0.4 1.1.0 1.1.0.1 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 All 162 releases
cartflows / assets / js / google-auto-fields.js

google-auto-fields.js in CartFlows – Funnel Builder & Checkout Plugin for WooCommerce 3.3.0, at assets/js/google-auto-fields.js

362 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 ( function ( $ ) {
2 let autocompleteShipping;
3 let autocompleteBilling;
4
5 const componentForm = {
6 street_number: 'long_name',
7 route: 'long_name',
8 locality: 'long_name',
9 postal_town: 'long_name',
10 sublocality_level_2: 'long_name',
11 administrative_area_level_1: 'short_name',
12 administrative_area_level_2: 'short_name',
13 country: 'short_name',
14 postal_code: 'short_name',
15 };
16
17 function handleAdminAreaLevel( fieldVal, elementId, default_state ) {
18 const sanitized_field_value = fieldVal.replace( /\s+/g, '' );
19 if (
20 $( `#${ elementId }` ).is( 'select' ) &&
21 $( `#${ elementId } option[value=${ sanitized_field_value }]` )
22 .length > 0
23 ) {
24 return sanitized_field_value;
25 }
26
27 return default_state;
28 }
29
30 function init_google_billing_address( country ) {
31 const address_field = document.getElementById( 'billing_address_1' );
32
33 // Bail if the field is not present in the checkout form.
34 if ( ! address_field ) {
35 return;
36 }
37
38 // Create the autocomplete object, restricting the search to geographical
39 // location types.
40
41 if ( country === undefined || country === null ) {
42 country = $( '#billing_country :selected' ).val();
43 }
44 const options = {
45 componentRestrictions: { country: [ country ] },
46 types: [ 'geocode' ],
47 };
48
49 // Remove country restriction if country field is disabled or not present in checkout form.
50 if ( '' === country || undefined === country ) {
51 delete options.componentRestrictions;
52 }
53
54 autocompleteBilling = new google.maps.places.Autocomplete(
55 address_field,
56 options
57 );
58
59 // When the user selects an address from the dropdown, populate the address
60 // fields in the form.
61 autocompleteBilling.addListener(
62 'place_changed',
63 autoFillParseAddressBilling
64 );
65 }
66
67 function init_google_shipping_address( country ) {
68 const address_field = document.getElementById( 'shipping_address_1' );
69
70 // Bail if the field is not present in the checkout form.
71 if ( ! address_field ) {
72 return;
73 }
74
75 // Create the autocomplete object, restricting the search to geographical
76 // location types.
77 if ( country === undefined || country === null ) {
78 country = $( '#shipping_country :selected' ).val();
79 }
80 const options = {
81 componentRestrictions: { country: [ country ] },
82 types: [ 'geocode' ],
83 };
84
85 // Remove country restriction if country field is disabled or not present in checkout form.
86 if ( '' === country || undefined === country ) {
87 delete options.componentRestrictions;
88 }
89
90 autocompleteShipping = new google.maps.places.Autocomplete(
91 address_field,
92 options
93 );
94
95 // When the user selects an address from the dropdown, populate the address
96 // fields in the form.
97 autocompleteShipping.addListener(
98 'place_changed',
99 autoFillParseAddressShipping
100 );
101 }
102
103 // Initialize places on billing country change.
104 $( document ).on( 'change', '#billing_country', function () {
105 const new_country = $( this ).val();
106 init_google_billing_address( new_country );
107 } );
108
109 // Initialize places on shipping country change.
110 $( document ).on( 'change', '#shipping_country', function () {
111 const new_country = $( this ).val();
112 init_google_shipping_address( new_country );
113 } );
114
115 // Function to parse the autofill value for billing fields.
116 function autoFillParseAddressBilling() {
117 $( '#billing_address_1' ).val( '' );
118 $( '#billing_country' ).val( '' );
119 $( '#billing_address_2' ).val( '' );
120 $( '#billing_city' ).val( '' );
121 $( '#billing_state' ).val( '' );
122 $( '#billing_postcode' ).val( '' );
123 // Get the place details from the autocomplete object.
124 const place = autocompleteBilling.getPlace();
125 const billing_add1_attr = [ 'street_number', 'route' ];
126 const billing_add2_attr = [ 'sublocality_level_2' ];
127
128 let billing_country = '',
129 state = '',
130 city = '',
131 billing_address_1 = '',
132 billing_address_2 = '',
133 postal_code = '';
134
135 // Get each component of the address from the place details
136 // and fill the corresponding field on the form.
137 if ( place && place.address_components.length > 0 ) {
138 for ( const address_value of place.address_components ) {
139 const addressType = address_value.types[ 0 ];
140
141 if ( componentForm[ addressType ] ) {
142 const fieldVal =
143 address_value[ componentForm[ addressType ] ];
144
145 if ( addressType === 'country' ) {
146 billing_country = fieldVal;
147 }
148
149 if (
150 addressType === 'administrative_area_level_1' ||
151 addressType === 'administrative_area_level_2'
152 ) {
153 state = handleAdminAreaLevel(
154 fieldVal,
155 'billing_state',
156 state
157 );
158 }
159
160 if (
161 addressType === 'locality' ||
162 addressType === 'postal_town'
163 ) {
164 city = fieldVal;
165 }
166
167 if ( addressType === 'postal_code' ) {
168 postal_code = fieldVal;
169 }
170
171 if ( billing_add1_attr.indexOf( addressType ) !== -1 ) {
172 billing_address_1 += fieldVal + ' ';
173 }
174
175 if ( billing_add2_attr.indexOf( addressType ) !== -1 ) {
176 billing_address_2 += fieldVal + ' ';
177 }
178 }
179 }
180
181 if ( $( '#billing_address_1' ).length > 0 ) {
182 if ( billing_address_1.length > 0 ) {
183 $( '#billing_address_1' )
184 .val( billing_address_1.trimEnd() )
185 .trigger( 'focus' );
186 }
187 }
188
189 if ( $( '#billing_address_2' ).length > 0 ) {
190 $( '#billing_address_2' )
191 .val( billing_address_2.trimEnd() )
192 .trigger( 'focus' );
193 }
194
195 if ( $( '#billing_postcode' ).length > 0 ) {
196 $( '#billing_postcode' ).val( postal_code ).trigger( 'focus' );
197 }
198
199 if ( $( '#billing_city' ).length > 0 ) {
200 $( '#billing_city' ).val( city ).trigger( 'focus' );
201 }
202
203 if ( $( '#billing_state' ).length > 0 ) {
204 $( '#billing_state' )
205 .val( state )
206 .trigger( 'focus' )
207 .trigger( 'change' );
208 }
209
210 if ( $( '#billing_country' ).length > 0 ) {
211 $( '#billing_country' )
212 .val( billing_country )
213 .trigger( 'focus' )
214 .trigger( 'change' );
215 }
216
217 /**
218 * Replace the billing_address_1 field value depending on the country's address format.
219 * Example:
220 * For US the format is street_number and street_route and
221 * for Netherlands the format is street_route and street_number.
222 * So replace it with the value if available.
223 */
224 if (
225 place.name &&
226 ( '' !== place.name || 'undefined' !== typeof place.name )
227 ) {
228 $( '#billing_address_1' ).val( place.name );
229 }
230 }
231 }
232
233 // Function to parse the autofill value for billing fields.
234 function autoFillParseAddressShipping() {
235 if ( $( '#ship-to-different-address-checkbox' ).is( ':checked' ) ) {
236 $( '#shipping_address_1' ).val( '' );
237 $( '#shipping_country' ).val( '' );
238 $( '#shipping_address_2' ).val( '' );
239 $( '#shipping_city' ).val( '' );
240 $( '#shipping_state' ).val( '' );
241 $( '#shipping_postcode' ).val( '' );
242 // Get the place details from the autocomplete object.
243 const place = autocompleteShipping.getPlace();
244 const shipping_add1_attr = [ 'street_number', 'route' ];
245 const shipping_add2_attr = [ 'sublocality_level_2' ];
246
247 let shipping_country = '',
248 state = '',
249 city = '',
250 shipping_address_1 = '',
251 shipping_address_2 = '',
252 postal_code = '';
253 // Get each component of the address from the place details
254 // and fill the corresponding field on the form.
255 if ( place && place.address_components.length > 0 ) {
256 for ( const address_value of place.address_components ) {
257 const addressType = address_value.types[ 0 ];
258 if ( componentForm[ addressType ] ) {
259 const fieldVal =
260 address_value[ componentForm[ addressType ] ];
261
262 if ( addressType === 'country' ) {
263 shipping_country = fieldVal;
264 }
265
266 if (
267 addressType === 'administrative_area_level_1' ||
268 addressType === 'administrative_area_level_2'
269 ) {
270 state = handleAdminAreaLevel(
271 fieldVal,
272 'shipping_state',
273 state
274 );
275 }
276
277 if (
278 addressType === 'locality' ||
279 addressType === 'postal_town'
280 ) {
281 city = fieldVal;
282 }
283
284 if ( addressType === 'postal_code' ) {
285 postal_code = fieldVal;
286 }
287
288 if (
289 shipping_add1_attr.indexOf( addressType ) !== -1
290 ) {
291 shipping_address_1 += fieldVal + ' ';
292 }
293
294 if (
295 shipping_add2_attr.indexOf( addressType ) !== -1
296 ) {
297 shipping_address_2 += fieldVal + ' ';
298 }
299 }
300 }
301
302 if ( $( '#shipping_address_1' ).length > 0 ) {
303 if ( shipping_address_1.length > 0 ) {
304 $( '#shipping_address_1' )
305 .val( shipping_address_1.trimEnd() )
306 .trigger( 'focus' );
307 }
308 }
309
310 if ( $( '#shipping_address_2' ).length > 0 ) {
311 $( '#shipping_address_2' )
312 .val( shipping_address_2.trimEnd() )
313 .trigger( 'focus' );
314 }
315
316 if ( $( '#shipping_postcode' ).length > 0 ) {
317 $( '#shipping_postcode' )
318 .val( postal_code )
319 .trigger( 'focus' );
320 }
321
322 if ( $( '#shipping_city' ).length > 0 ) {
323 $( '#shipping_city' ).val( city ).trigger( 'focus' );
324 }
325
326 if ( $( '#shipping_state' ).length > 0 ) {
327 $( '#shipping_state' )
328 .val( state )
329 .trigger( 'focus' )
330 .trigger( 'change' );
331 }
332
333 if ( $( '#shipping_country' ).length > 0 ) {
334 $( '#shipping_country' )
335 .val( shipping_country )
336 .trigger( 'focus' )
337 .trigger( 'change' );
338 }
339
340 /**
341 * Replace the shipping_address_1 field value depending on the country's address format.
342 * Example:
343 * For US the format is street_number and street_route and
344 * for Netherlands the format is street_route and street_number.
345 * So replace it with the value if available.
346 */
347 if (
348 place.name &&
349 ( '' !== place.name || 'undefined' !== typeof place.name )
350 ) {
351 $( '#shipping_address_1' ).val( place.name );
352 }
353 }
354 }
355 }
356
357 $( function () {
358 init_google_billing_address();
359 init_google_shipping_address();
360 } );
361 } )( jQuery );
362