Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
snippets:shipping:restricting_countries_and_states_on_the_checkout [2013/03/21 05:13] – [Step 1: Add Javascript] adam | snippets:shipping:restricting_countries_and_states_on_the_checkout [2017/04/26 07:02] (current) – external edit 127.0.0.1 | ||
---|---|---|---|
Line 4: | Line 4: | ||
name : Restricting Countries and States on the Checkout | name : Restricting Countries and States on the Checkout | ||
description | description | ||
- | versions_tags : 1.0 #compatible versions | + | versions_tags : 1.0, 1.1 #compatible versions |
tags_tags | tags_tags | ||
date_dt | date_dt | ||
Line 12: | Line 12: | ||
</ | </ | ||
+ | |||
+ | |||
+ | <WRAP center round important 80%> | ||
+ | **Using version 2.0?** There is native functionality that covers this snippet within the configuration section of the administration. There is also a new snippet available for our latest version, [[v: | ||
+ | </ | ||
====== Restricting Countries and States on the Checkout | ====== Restricting Countries and States on the Checkout | ||
Line 24: | Line 29: | ||
Add the following right before the closing ''</ | Add the following right before the closing ''</ | ||
- | <code javascript>< | + | <code javascript>< |
//< | //< | ||
jQuery(document).ready(function() { | jQuery(document).ready(function() { | ||
Line 39: | Line 44: | ||
</ | </ | ||
- | <script type="text/ | + | <script type='text/ |
//< | //< | ||
// Country/ | // Country/ | ||
Line 45: | Line 50: | ||
FC.locations.removeCountries = function(countries, | FC.locations.removeCountries = function(countries, | ||
- | if (typeof countries == "undefined") { return false } | + | if (typeof countries == 'undefined') { return false } |
- | if (typeof countries == "string") { countries = [countries]; | + | if (typeof countries == 'string') { countries = [countries]; |
locationArrayNames = FC.locations.validateLocationArrayNames(locationArrayNames); | locationArrayNames = FC.locations.validateLocationArrayNames(locationArrayNames); | ||
Line 52: | Line 57: | ||
var locationArray = FC.locations.getLocationArray(locationArrayNames[l]); | var locationArray = FC.locations.getLocationArray(locationArrayNames[l]); | ||
for (var c in countries) { | for (var c in countries) { | ||
- | if (typeof locationArray[countries[c]] == "undefined") { break; } | + | if (typeof locationArray[countries[c]] == 'undefined') { break; } |
delete locationArray[countries[c]]; | delete locationArray[countries[c]]; | ||
} | } | ||
Line 61: | Line 66: | ||
FC.locations.limitCountriesTo = function(countries, | FC.locations.limitCountriesTo = function(countries, | ||
- | if (typeof countries == "undefined") { return false } | + | if (typeof countries == 'undefined') { return false } |
- | if (typeof countries == "string") { countries = [countries]; | + | if (typeof countries == 'string') { countries = [countries]; |
locationArrayNames = FC.locations.validateLocationArrayNames(locationArrayNames); | locationArrayNames = FC.locations.validateLocationArrayNames(locationArrayNames); | ||
Line 69: | Line 74: | ||
var locationArray = FC.locations.getLocationArray(locationArrayNames[l]); | var locationArray = FC.locations.getLocationArray(locationArrayNames[l]); | ||
for (var c in countries) { | for (var c in countries) { | ||
- | if (typeof locationArray[countries[c]] == "undefined") { break; } | + | if (typeof locationArray[countries[c]] == 'undefined') { break; } |
newLocations[countries[c]] = locationArray[countries[c]]; | newLocations[countries[c]] = locationArray[countries[c]]; | ||
} | } | ||
Line 76: | Line 81: | ||
if (newLocations == {}) { return false; } | if (newLocations == {}) { return false; } | ||
- | if (locationArrayNames[l] == "customer") { | + | if (locationArrayNames[l] == 'customer') { |
FC.locations.config.locations = newLocations; | FC.locations.config.locations = newLocations; | ||
} else { | } else { | ||
Line 86: | Line 91: | ||
FC.locations.removeStates = function(country, | FC.locations.removeStates = function(country, | ||
- | if (typeof country == "undefined" | + | if (typeof country == 'undefined' |
- | if (typeof states == "string") { states = [states]; } | + | if (typeof states == 'string') { states = [states]; } |
locationArrayNames = FC.locations.validateLocationArrayNames(locationArrayNames); | locationArrayNames = FC.locations.validateLocationArrayNames(locationArrayNames); | ||
for (l in locationArrayNames) { | for (l in locationArrayNames) { | ||
var locationArray = FC.locations.getLocationArray(locationArrayNames[l]); | var locationArray = FC.locations.getLocationArray(locationArrayNames[l]); | ||
- | if (typeof locationArray[country] == "undefined") { return false; } | + | if (typeof locationArray[country] == 'undefined') { return false; } |
for (var s in states) { | for (var s in states) { | ||
- | if (typeof locationArray[country].r[states[s]] == "undefined") { break; } | + | if (typeof locationArray[country].r[states[s]] == 'undefined') { break; } |
delete locationArray[country].r[states[s]]; | delete locationArray[country].r[states[s]]; | ||
} | } | ||
Line 102: | Line 107: | ||
FC.locations.limitStatesTo = function(country, | FC.locations.limitStatesTo = function(country, | ||
- | if (typeof country == "undefined" | + | if (typeof country == 'undefined' |
- | if (typeof states == "string") { states = [states]; } | + | if (typeof states == 'string') { states = [states]; } |
locationArrayNames = FC.locations.validateLocationArrayNames(locationArrayNames); | locationArrayNames = FC.locations.validateLocationArrayNames(locationArrayNames); | ||
Line 109: | Line 114: | ||
var newLocations = {}; | var newLocations = {}; | ||
var locationArray = FC.locations.getLocationArray(locationArrayNames[l]); | var locationArray = FC.locations.getLocationArray(locationArrayNames[l]); | ||
- | if (typeof locationArray[country] == "undefined") { return false; } | + | if (typeof locationArray[country] == 'undefined') { return false; } |
for (var s in states) { | for (var s in states) { | ||
- | if (typeof locationArray[country].r[states[s]] == "undefined") { break; } | + | if (typeof locationArray[country].r[states[s]] == 'undefined') { break; } |
newLocations[states[s]] = locationArray[country].r[states[s]]; | newLocations[states[s]] = locationArray[country].r[states[s]]; | ||
} | } | ||
- | if (locationArrayNames[l] == "customer") { | + | if (locationArrayNames[l] == 'customer') { |
FC.locations.config.locations[country].r = newLocations; | FC.locations.config.locations[country].r = newLocations; | ||
} else { | } else { | ||
Line 127: | Line 132: | ||
FC.checkout.config.evaluateAjaxRequests = false; | FC.checkout.config.evaluateAjaxRequests = false; | ||
- | FC.checkout.setAutoComplete("customer_country"); | + | FC.checkout.setAutoComplete('customer_country'); |
- | if (jQuery("# | + | if (jQuery('# |
- | FC.checkout.validateLocationName("customer_country"); | + | FC.checkout.validateLocationName('customer_country'); |
} | } | ||
- | if (jQuery("# | + | if (jQuery('# |
- | FC.checkout.validateLocationName("customer_state"); | + | FC.checkout.validateLocationName('customer_state'); |
} | } | ||
if (blockErrors) { | if (blockErrors) { | ||
- | FC.checkout.updateErrorDisplay("customer_country_name", false); | + | FC.checkout.updateErrorDisplay('customer_country_name', false); |
- | FC.checkout.updateErrorDisplay("customer_state_name", false); | + | FC.checkout.updateErrorDisplay('customer_state_name', false); |
} | } | ||
if (!FC.checkout.config.hasMultiship) { | if (!FC.checkout.config.hasMultiship) { | ||
- | FC.checkout.setAutoComplete("shipping_country"); | + | FC.checkout.setAutoComplete('shipping_country'); |
- | if (jQuery("# | + | if (jQuery('# |
- | FC.checkout.validateLocationName("shipping_country"); | + | FC.checkout.validateLocationName('shipping_country'); |
} | } | ||
- | if (jQuery("# | + | if (jQuery('# |
- | FC.checkout.validateLocationName("shipping_state"); | + | FC.checkout.validateLocationName('shipping_state'); |
} | } | ||
if (blockErrors) { | if (blockErrors) { | ||
- | FC.checkout.updateErrorDisplay("shipping_country_name", false); | + | FC.checkout.updateErrorDisplay('shipping_country_name', false); |
- | FC.checkout.updateErrorDisplay("shipping_state_name", false); | + | FC.checkout.updateErrorDisplay('shipping_state_name', false); |
} | } | ||
Line 156: | Line 161: | ||
} else { | } else { | ||
for (var i = 0; i < FC.checkout.config.multishipDetails.length; | for (var i = 0; i < FC.checkout.config.multishipDetails.length; | ||
- | FC.checkout.setAutoComplete("shipto_" | + | FC.checkout.setAutoComplete('shipto_' |
- | if (jQuery("#shipto_" | + | if (jQuery('#shipto_' |
- | FC.checkout.validateLocationName("shipto_" | + | FC.checkout.validateLocationName('shipto_' |
} | } | ||
- | if (jQuery("#shipto_" | + | if (jQuery('#shipto_' |
- | FC.checkout.validateLocationName("shipto_" | + | FC.checkout.validateLocationName('shipto_' |
} | } | ||
if (blockErrors) { | if (blockErrors) { | ||
- | FC.checkout.updateErrorDisplay("shipto_" | + | FC.checkout.updateErrorDisplay('shipto_' |
- | FC.checkout.updateErrorDisplay("shipto_" | + | FC.checkout.updateErrorDisplay('shipto_' |
} | } | ||
} | } | ||
Line 178: | Line 183: | ||
FC.locations.getLocationArray = function(locationArrayNames) { | FC.locations.getLocationArray = function(locationArrayNames) { | ||
- | return (locationArrayNames == "customer") ? FC.locations.config.locations : FC.locations.config.shippingLocations; | + | return (locationArrayNames == 'customer') ? FC.locations.config.locations : FC.locations.config.shippingLocations; |
} | } | ||
FC.locations.validateLocationArrayNames = function(locationArrayNames) { | FC.locations.validateLocationArrayNames = function(locationArrayNames) { | ||
- | if (typeof locationArrayNames == "undefined" | + | if (typeof locationArrayNames == 'undefined' |
- | if (typeof locationArrayNames == "string") { locationArrayNames = [locationArrayNames]; | + | if (typeof locationArrayNames == 'string') { locationArrayNames = [locationArrayNames]; |
return locationArrayNames; | return locationArrayNames; | ||
} | } | ||
Line 197: | Line 202: | ||
**Parameters: | **Parameters: | ||
* '' | * '' | ||
- | * '' | + | * '' |
**Example: | **Example: | ||
- | * '' | + | * '' |
- | * '' | + | * '' |
---- | ---- | ||
Line 210: | Line 215: | ||
**Parameters: | **Parameters: | ||
* '' | * '' | ||
- | * '' | + | * '' |
**Example: | **Example: | ||
- | * '' | + | * '' |
- | * '' | + | * '' |
---- | ---- | ||
Line 224: | Line 229: | ||
* '' | * '' | ||
* '' | * '' | ||
- | * '' | + | * '' |
**Example: | **Example: | ||
- | * '' | + | * '' |
- | * '' | + | * '' |
---- | ---- | ||
Line 239: | Line 244: | ||
* '' | * '' | ||
* '' | * '' | ||
- | * '' | + | * '' |
**Example: | **Example: | ||
- | * '' | + | * '' |
- | * '' | + | * '' |
---- | ---- | ||
Line 267: | Line 272: | ||
- Restricting the checkout to only allow shipping to the United States, but allow billing to be from any country | - Restricting the checkout to only allow shipping to the United States, but allow billing to be from any country | ||
- | <code javascript> | + | <code javascript> |
FC.checkout.requireShippingAddress();</ | FC.checkout.requireShippingAddress();</ | ||
Line 275: | Line 280: | ||
- Only allow customers from the US and Canada, and not from Alaska or Hawaii. | - Only allow customers from the US and Canada, and not from Alaska or Hawaii. | ||
- | <code javascript> | + | <code javascript> |
- | FC.locations.removeStates("US", ["AK", "HI"]);</ | + | FC.locations.removeStates('US', ['AK', 'HI']);</ |
Line 283: | Line 288: | ||
- Remove all non-standard US States | - Remove all non-standard US States | ||
- | <code javascript> | + | <code javascript> |
Line 291: | Line 296: | ||
- Remove countries that are currently under sanction restrictions from the US Office of Foreign Assets Control (Note that this list may not be up to date) | - Remove countries that are currently under sanction restrictions from the US Office of Foreign Assets Control (Note that this list may not be up to date) | ||
- | <code javascript> | + | <code javascript> |
==== What about making it read-only? ==== | ==== What about making it read-only? ==== | ||
Line 300: | Line 305: | ||
* 2012/05/09 - v1.0 - Initial version | * 2012/05/09 - v1.0 - Initial version | ||
+ | * 2013/03/19 - v1.1 - Changed '' |