| @@ -27,8 +27,15 @@ | ||
| 27 | 27 | return default_state; |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 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 | + | |
| 31 | 38 | // Create the autocomplete object, restricting the search to geographical |
| 32 | 39 | // location types. |
| 33 | 40 | |
| 34 | 41 | if ( country === undefined || country === null ) { |
| @@ -44,9 +51,9 @@ | ||
| 44 | 51 | delete options.componentRestrictions; |
| 45 | 52 | } |
| 46 | 53 | |
| 47 | 54 | autocompleteBilling = new google.maps.places.Autocomplete( |
| 48 | - document.getElementById( 'billing_address_1' ), | |
| 55 | + address_field, | |
| 49 | 56 | options |
| 50 | 57 | ); |
| 51 | 58 | |
| 52 | 59 | // When the user selects an address from the dropdown, populate the address |
| @@ -57,8 +64,15 @@ | ||
| 57 | 64 | ); |
| 58 | 65 | } |
| 59 | 66 | |
| 60 | 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 | + | |
| 61 | 75 | // Create the autocomplete object, restricting the search to geographical |
| 62 | 76 | // location types. |
| 63 | 77 | if ( country === undefined || country === null ) { |
| 64 | 78 | country = $( '#shipping_country :selected' ).val(); |
| @@ -73,9 +87,9 @@ | ||
| 73 | 87 | delete options.componentRestrictions; |
| 74 | 88 | } |
| 75 | 89 | |
| 76 | 90 | autocompleteShipping = new google.maps.places.Autocomplete( |
| 77 | - document.getElementById( 'shipping_address_1' ), | |
| 91 | + address_field, | |
| 78 | 92 | options |
| 79 | 93 | ); |
| 80 | 94 | |
| 81 | 95 | // When the user selects an address from the dropdown, populate the address |
| @@ -259,9 +273,12 @@ | ||
| 259 | 273 | state |
| 260 | 274 | ); |
| 261 | 275 | } |
| 262 | 276 | |
| 263 | - if ( addressType === 'locality' ) { | |
| 277 | + if ( | |
| 278 | + addressType === 'locality' || | |
| 279 | + addressType === 'postal_town' | |
| 280 | + ) { | |
| 264 | 281 | city = fieldVal; |
| 265 | 282 | } |
| 266 | 283 | |
| 267 | 284 | if ( addressType === 'postal_code' ) { |
| @@ -336,9 +353,9 @@ | ||
| 336 | 353 | } |
| 337 | 354 | } |
| 338 | 355 | } |
| 339 | 356 | |
| 340 | - $( document ).on( 'ready', function () { | |
| 357 | + $( function () { | |
| 341 | 358 | init_google_billing_address(); |
| 342 | 359 | init_google_shipping_address(); |
| 343 | 360 | } ); |
| 344 | 361 | } )( jQuery ); |