Skip to main content

Addressfinder plugin for StoreConnect

Installation

Step 1 - Creating a new script block

  1. Open the App Launcher.
  2. In the search bar, search and select 'Script Blocks'.

StoreConnect app launcher installation

  1. Create a new script block.

StoreConnect selecting a new script block

Step 2 - Populate the new script block

  1. Give the script block a name (eg: Addressfinder).
  2. Choose the store you would like to have the Addressfinder service running on.
  3. Copy our javascript code and paste it into the 'Content' field.
  4. Save.

StoreConnect how to populate a new script block

Step 3 - Testing

  1. Go to your website checkout page and reload the page. Then start typing an address into the Street Address field.
  2. Check that addresses are being displayed in a drop down box below the Street Address field.
  3. Select an address and check that the address information populates the correct fields.
  4. Test at least one unit type address (Eg, 3/22 Broadway, BONBEACH VIC 3196) to make sure the unit and street information populate correctly.

StoreConnect testing addressfinder autocomplete


Addressfinder javascript code for StoreConnect

(function(d) {
var stateSelector;
var trustedEvent = true;

var conf = {
// Important: Replace the Demo key below with in your Addressfinder licence key
licenceKey: "ADDRESSFINDER_DEMO_KEY",

defaultCountryCode: "AU",
countryMatchAttribute: "value",

addressSelector: "#shipping_address_line_1",
suburbSelector: "#shipping_address_line_2",
citySelector: "#shipping_city__customer_information",
stateSelector: "#shipping_state__customer_information",
postcodeSelector: "#shipping_postal_code__customer_information",
countrySelector: "#shipping_country__customer_information",

countryConfig: {
AU: {
widgetOptions: {
address_params: {
source: 'gnaf,paf',
po_box: '0'
}
},
},
IE: {
stateAttributes: {
"County Cavan": "CN",
"County Clare": "CE",
"County Westmeath": "WH",
"County Longford": "LD",
"County Munster": "M",
"County Meath": "MH",
"County Dublin": "D",
"County Carlow": "CW",
"County Kerry": "KY",
"County Kilkenny": "KK",
"County Kildare": "KE",
"County Wicklow": "WW",
"County Connaught": "C",
"County Cork": "CO",
"County Donegal": "DL",
"County Galway": "G",
"County Leinster": "L",
"County Laois": "LS",
"County Leitrim": "LM",
"County Limerick": "LK",
"County Wexford": "WX",
"County Louth": "LH",
"County Mayo": "MO",
"County Monaghan": "MN",
"County Offaly": "OY",
"County Waterford": "WD",
"County Roscommon": "RN",
"County Sligo": "SO",
"County Tipperary": "TA",
"County Ulster": "U"
}
},
ES: {
stateAttributes: {
"A Coruña": "C",
"Alacant": "A",
"Albacete": "AB",
"Almería": "AL",
"Andalucía": "AN",
"Araba": "VI",
"Aragón": "AR",
"Asturias": "O",
"Principado de Asturias": "AS",
"Badajoz": "BA",
"Barcelona": "B",
"Bizkaia": "BI",
"Burgos": "BU",
"Canarias": "CN",
"Cantabria": "S",
"Cantabria": "CB",
"Castellón": "CS",
"Castilla y León": "CL",
"Castilla-La Mancha": "CM",
"Catalunya": "CT",
"Ceuta": "CE",
"Ciudad Real": "CR",
"Cuenca": "CU",
"Cáceres": "CC",
"Cádiz": "CA",
"Córdoba": "CO",
"Euskal Herria": "PV",
"Extremadura": "EX",
"Galicia": "GA",
"Gipuzkoa": "SS",
"Girona": "GI",
"Granada": "GR",
"Guadalajara": "GU",
"Huelva": "H",
"Huesca": "HU",
"Illes Balears": "PM",
"Illes Balears": "IB",
"Jaén": "J",
"La Rioja": "LO",
"La Rioja": "RI",
"Las Palmas": "GC",
"León": "LE",
"Lleida": "L",
"Lugo": "LU",
"Madrid": "M",
"Comunidad de Madrid": "MD",
"Melilla": "ML",
"Murcia": "MU",
"Región de Murcia": "MC",
"Málaga": "MA",
"Nafarroa": "NA",
"Nafarroako Foru Komunitatea": "NC",
"Ourense": "OR",
"Palencia": "P",
"Pontevedra": "PO",
"Salamanca": "SA",
"Santa Cruz de Tenerife": "TF",
"Segovia": "SG",
"Sevilla": "SE",
"Soria": "SO",
"Tarragona": "T",
"Teruel": "TE",
"Toledo": "TO",
"Valencia": "V",
"Comunidad Valenciana": "VC",
"Valladolid": "VA",
"Zamora": "ZA",
"Zargoza": "Z",
"Ávila": "AV"
}
},
NZ: {
stateAttributes: {
"Auckland Region": "AUK",
"Bay of Plenty Region": "BOP",
"Canterbury Region": "CAN",
"Area Outside Region": "CIT",
"Gisborne Region": "GIS",
"Hawke's Bay Region": "HKB",
"Manawatū-Whanganui Region": "MWT",
"Marlborough Region": "MBH",
"Southland Region": "STL",
"Nelson Region": "NSN",
"Northland Region": "NTL",
"Otago Region": "OTA",
"Taranaki Region": "TKI",
"Tasman Region": "TAS",
"West Coast Region": "WTC",
"Wellington Region": "WGN",
"Waikato Region": "WKO"
}
}
},

events: {
ready: function(event) {
event.target.on("address:select", function(fullAddress, metadata) {
// this block only runs when the country select element was not changed by the user.
if (!trustedEvent) {
setTimeout(() => {
let countryCode = event.target.country.country_code
switch (countryCode) {
case 'au':
stateSelector.value = metadata.state_territory
break
case 'nz':
stateSelector.value = findMappedState(conf.countryConfig['NZ'].stateAttributes, metadata.region)
break
case 'ie':
case 'es':
stateSelector.value = findMappedState(conf.countryConfig[countryCode.toUpperCase()].stateAttributes, metadata.address.state)
break
default:
stateSelector.value = metadata.address.state
}
}, 1000);
}
})
}
},

debugMode: true,
};

// return the state value found in the select option element.
function findMappedState(states, value) {
return states[value]
}

document.addEventListener("DOMContentLoaded", function() {
if (document.querySelector(conf.addressSelector)) {
stateSelector = document.querySelector(conf.stateSelector);
const countrySelector = document.querySelector(conf.countrySelector);

countrySelector.addEventListener('change', (event) => {
// trustedEvent is true when the event was generated by a user action,
// and false when the event was created or modified by a script or dispatched via EventTarget.dispatchEvent().
trustedEvent = event.isTrusted
});

var e = document.createElement("script");
e.src = "https://api.addressfinder.io/assets/generic/v1/address.js", e.async = !0, e.onload = function() {

new Addressfinder.Address.BootHandler(conf)
}, document.body.appendChild(e)
}
});
})(document);