Restrict Country by Field

Restrict autocomplete results to a specific country (or countries) for a specific field.

Instructions

  1. Install this snippet with our free Custom JavaScript plugin. https://gravitywiz.com/gravity-forms-code-chest/

  2. Follow the inline instructions to configure this for your specific field.

Code

Filename: gpaa-restrict-country-by-field.js

/**
 * Gravity Perks // Address Autocomplete // Restrict Country by Field
 * https://gravitywiz.com/documentation/gravity-forms-address-autocomplete/
 *
 * Restrict autocomplete results to a specific country (or countries) for a specific field.
 *
 * Instructions:
 *
 * 1. Install this snippet with our free Custom JavaScript plugin.
 *    https://gravitywiz.com/gravity-forms-code-chest/
 *
 * 2. Follow the inline instructions to configure this for your specific field.
 */
gform.addFilter( 'gpaa_autocomplete_options', function( autocompleteOptions, gpaa, formId, fieldId ) {
	// Update "1" to your Address field ID.
	if ( formId != GFFORMID || fieldId != 1 ) {
		return autocompleteOptions;
	}
	if ( typeof autocompleteOptions.componentRestrictions !== 'object' ) {
		autocompleteOptions.componentRestrictions = {};
	}
	// Update "DE" to the country to which you would like to restrict results.
	autocompleteOptions.componentRestrictions.country = [ 'DE' ];
	return autocompleteOptions;
} );

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.