PluginProbe
PostNL for WooCommerce / 4.0.0
PostNL for WooCommerce v4.0.0
5.9.12 5.9.11 5.9.10 5.9.9 5.9.8 5.9.7 5.9.6 trunk 2.5.0 2.5.1 2.5.2 2.5.3 2.5.4 2.5.5 3.1.4 3.1.5 3.1.6 3.1.7 4.0.0 4.0.1 4.0.2 4.3.2 4.3.3 4.4.0 4.4.1 All 72 releases
woo-postnl / assets / js / wcpn-checkout-fields.js

wcpn-checkout-fields.js in PostNL for WooCommerce 4.0.0, at assets/js/wcpn-checkout-fields.js

68 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 /**
2 * Loaded in checkout when using split address fields.
3 *
4 * Compatible with 5.6 and 7.1 version of the plugin.
5 */
6 window.addEventListener('load', function() {
7 var addressRegex = /(.*?)\s?(\d{1,4})[/\s-]{0,2}([a-zA-Z]\d{1,3}|-\d{1,4}|\d{2}\w{1,2}|[a-zA-Z][a-zA-Z\s]{0,3})?$/g;
8
9 var billingStreet = document.querySelector('#billing_street_name');
10 var shippingStreet = document.querySelector('#shipping_street_name');
11
12 /**
13 * Trigger hiding of address line 1 and 2 if necessary. The timeout is necessary, otherwise the order summary is going
14 * to flash.
15 *
16 * It checks for PostNL_Frontend before running as that script has the same code.
17 */
18 if (!window.hasOwnProperty('PostNL_Frontend')) {
19 setTimeout(function() {
20 var event = document.createEvent('HTMLEvents');
21 event.initEvent('change', true, false);
22 document.querySelectorAll('.country_to_state').forEach(function(selector) {
23 selector.dispatchEvent(event);
24 });
25 }, 100);
26 }
27
28 /**
29 * Set the correct autocomplete attribute on the street fields if none is present.
30 */
31 [billingStreet, shippingStreet].forEach(function(el) {
32 if (!el.getAttribute('autocomplete')) {
33 el.setAttribute('autocomplete', 'street-address');
34 }
35 });
36
37 billingStreet.addEventListener('load', setAddress);
38 billingStreet.addEventListener('animationend', setAddress);
39
40 shippingStreet.addEventListener('load', setAddress);
41 shippingStreet.addEventListener('animationend', setAddress);
42
43 /**
44 * Autofill helper for layout with separate number field.
45 * Split street to 3 fields on autofill.
46 */
47 function setAddress() {
48 var type = this.getAttribute('id').search('billing') ? 'shipping' : 'billing';
49
50 /* filter out empty values */
51 var address = this.value.split(addressRegex).filter(function(value) {
52 return !!value;
53 });
54
55 /* Fill in the hidden address line 1 field in case a theme forces it to be required */
56 document.querySelector('#' + type + '_address_1').value = this.value;
57
58 document.querySelector('#' + type + '_street_name').value = address[0] || '';
59 document.querySelector('#' + type + '_house_number').value = address[1] || '';
60 document.querySelector('#' + type + '_house_number_suffix').value = address[2] || '';
61
62 /* Update settings after filling if the frontend script is loaded and initialized and address is not empty */
63 if (address.length && window.hasOwnProperty('PostNL_Frontend')) {
64 window.MyParcel_Frontend.updateAddress();
65 }
66 }
67 });
68