PluginProbe
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce / 3.1.2
CartFlows – Funnel Builder & Checkout Plugin for WooCommerce v3.1.2
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.1.2, at assets/js/google-auto-fields.js

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