Always Use Short Name for State / Province / Region

This was develope for a customer using Address Autocomplete in Italy. The province was retuned as “Città Metropolitana di Torino” where they wanted to use the province code instead (e.g. “TO”).

Instructions

Code

Filename: gpaa-use-short-name-for-province.js

/**
 * Gravity Perks // Address Autocomplete // Always Use Short Name for State / Province / Region
 * https://gravitywiz.com/documentation/gravity-forms-address-autocompate/
 *
 * Instruction Video: https://www.loom.com/share/f39708854d504d32902b5fca29e73213
 *
 * This was develope for a customer using Address Autocomplete in Italy. The province was retuned as
 * "Città Metropolitana di Torino" where they wanted to use the province code instead (e.g. "TO").
 */
gform.addFilter("gpaa_values", function (values, place) {
  for (var i = 0; i < place.address_components.length; i++) {
    var component = place.address_components[i];
    if (component.long_name === values.stateProvince) {
      values.stateProvince = component.short_name;
    }
  }
  return values;
});

Leave a Reply

Your email address will not be published. Required fields are marked *

  • Trouble installing this snippet? See our troubleshooting tips.
  • Need to include code? Create a gist and link to it in your comment.
  • Reporting a bug? Provide a URL where this issue can be recreated.

By commenting, I understand that I may receive emails related to Gravity Wiz and can unsubscribe at any time.