Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
snippets:shipping:restricting_countries_and_states_on_the_checkout [2012/10/08 05:11] – Links to snippets:shipping:restricing_countries_and_states_on_the_checkout_old changed to snippets:shipping:restricting_countries_and_states_on_the_checkout_old adamsnippets: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 about snippet description   :  #description about snippet
-versions_tags : 1.0 #compatible versions+versions_tags : 1.0, 1.1 #compatible versions
 tags_tags     : snippets, shipping, advance #tags, separated by commas. tags_tags     : snippets, shipping, advance #tags, separated by commas.
 date_dt       : 2012-09-05 #the date in YYYY-MM-DD format date_dt       : 2012-09-05 #the date in YYYY-MM-DD format
Line 12: Line 12:
 </WRAP> </WRAP>
  
 +
 +
 +<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:2.0:snippets:locations_modfication|available from here]].
 +</WRAP>
  
 ====== Restricting Countries and States on the Checkout  ====== ====== Restricting Countries and States on the Checkout  ======
Line 19: Line 24:
 <wrap important>The functionality described on this page require advanced javascript knowledge, and are not officially supported.</wrap> They are included in our official wiki because certain shipping methods and functionality are not natively supported, and though we are working on radically improving our shipping functionality, in the meantime these methods may be great workarounds. Use with caution, test, and post in [[http://forum.foxycart.com/|our forum]] if you run into problems. <wrap important>The functionality described on this page require advanced javascript knowledge, and are not officially supported.</wrap> They are included in our official wiki because certain shipping methods and functionality are not natively supported, and though we are working on radically improving our shipping functionality, in the meantime these methods may be great workarounds. Use with caution, test, and post in [[http://forum.foxycart.com/|our forum]] if you run into problems.
  
-See the [[snippets:shipping#changelog|changelog]] for details on updates to this script.+See the [[#changelog|changelog]] for details on updates to this script.
 ==== Step 1: Add Javascript ==== ==== Step 1: Add Javascript ====
  
 Add the following right before the closing ''</head>'' tag in your checkout template: Add the following right before the closing ''</head>'' tag in your checkout template:
  
-<code javascript><script type="text/javascriptcharset="utf-8">+<code javascript><script type='text/javascriptcharset='utf-8'>
  //<![CDATA[  //<![CDATA[
  jQuery(document).ready(function() {  jQuery(document).ready(function() {
Line 39: Line 44:
 </script> </script>
  
-<script type="text/javascriptcharset="utf-8">+<script type='text/javascriptcharset='utf-8'>
  //<![CDATA[  //<![CDATA[
- // Country/State Helper Functions v1.0+ // Country/State Helper Functions v1.1
  // Do not modify the following functions  // Do not modify the following functions
  
  FC.locations.removeCountries = function(countries, locationArrayNames) {  FC.locations.removeCountries = function(countries, locationArrayNames) {
- 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, locationArrayNames) {  FC.locations.limitCountriesTo = function(countries, locationArrayNames) {
- 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, states, locationArrayNames) {  FC.locations.removeStates = function(country, states, locationArrayNames) {
- if (typeof country == "undefined|| typeof states == "undefined") { return false } + if (typeof country == 'undefined|| typeof states == 'undefined') { return false } 
- 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, states, locationArrayNames) {  FC.locations.limitStatesTo = function(country, states, locationArrayNames) {
- if (typeof country == "undefined|| typeof states == "undefined") { return false } + if (typeof country == 'undefined|| typeof states == 'undefined') { return false } 
- 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 125: Line 130:
    
  FC.locations.updateFoxyComplete = function(blockErrors) {  FC.locations.updateFoxyComplete = function(blockErrors) {
- FC.checkout.setAutoComplete("customer_country"); + FC.checkout.config.evaluateAjaxRequests = false; 
- if (jQuery("#customer_country_name") != "") { + 
- FC.checkout.validateLocationName("customer_country");+ FC.checkout.setAutoComplete('customer_country'); 
 + if (jQuery('#customer_country_name') != '') { 
 + FC.checkout.validateLocationName('customer_country');
  }  }
- if (jQuery("#customer_state_name").val() != "") { + if (jQuery('#customer_state_name').val() != '') { 
- 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("#shipping_country_name") != "") { + if (jQuery('#shipping_country_name') != '') { 
- FC.checkout.validateLocationName("shipping_country");+ FC.checkout.validateLocationName('shipping_country');
  }  }
- if (jQuery("#shipping_state_name") != "") { + if (jQuery('#shipping_state_name') != '') { 
- 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);
  }  }
 +
 + FC.checkout.config.evaluateAjaxRequests = true;
 + FC.checkout.updateShipping(-1);
 + FC.checkout.updateTaxes(-1);
  } else {  } else {
  for (var i = 0; i < FC.checkout.config.multishipDetails.length; i++) {  for (var i = 0; i < FC.checkout.config.multishipDetails.length; i++) {
- FC.checkout.setAutoComplete("shipto_+ i + "_country"); + FC.checkout.setAutoComplete('shipto_+ i + '_country'); 
- if (jQuery("#shipto_+ i + "_country_name") != "") { + if (jQuery('#shipto_+ i + '_country_name') != '') { 
- FC.checkout.validateLocationName("shipto_+ i + "_country");+ FC.checkout.validateLocationName('shipto_+ i + '_country');
  }  }
- if (jQuery("#shipto_+ i + "_state_name") != "") { + if (jQuery('#shipto_+ i + '_state_name') != '') { 
- FC.checkout.validateLocationName("shipto_+ i + "_state");+ FC.checkout.validateLocationName('shipto_+ i + '_state');
  }  }
  if (blockErrors) {  if (blockErrors) {
- FC.checkout.updateErrorDisplay("shipto_+ i + "_country_name", false); + FC.checkout.updateErrorDisplay('shipto_+ i + '_country_name', false); 
- FC.checkout.updateErrorDisplay("shipto_+ i + "_state_name", false);+ FC.checkout.updateErrorDisplay('shipto_+ i + '_state_name', false);
  }  }
 + }
 +
 + FC.checkout.config.evaluateAjaxRequests = true;
 + for (var i = 0; i < FC.checkout.config.multishipDetails.length; i++) {
 + FC.checkout.updateShipping(i);
 + FC.checkout.updateTaxes(i);
  }  }
  }  }
Line 166: 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|| locationArrayNames == "" || locationArrayNames == "both") { locationArrayNames = ["customer""shipping"]; } + if (typeof locationArrayNames == 'undefined|| locationArrayNames == '' || locationArrayNames == 'both') { locationArrayNames = ['customer''shipping']; } 
- if (typeof locationArrayNames == "string") { locationArrayNames = [locationArrayNames]; }+ if (typeof locationArrayNames == 'string') { locationArrayNames = [locationArrayNames]; }
  return locationArrayNames;  return locationArrayNames;
  }  }
  //]]>  //]]>
 </script></code> </script></code>
- 
 ==== Step 2: Add Custom Location Rules ==== ==== Step 2: Add Custom Location Rules ====
  
Line 186: Line 202:
 **Parameters:**  **Parameters:** 
   * ''countries'' //<wrap lo>(String or Array)</wrap>// - Either a string or an array of strings of the 2 character ISO country codes   * ''countries'' //<wrap lo>(String or Array)</wrap>// - Either a string or an array of strings of the 2 character ISO country codes
-  * ''locationArrayNames'' //<wrap lo>(String or Array)</wrap>// - //Optional// - Either a string of either ''"customer"'', ''"shipping"'' or ''"both"'', or an array of both like ''["customer""shipping"]''. If not specified, ''"both"'' is the automatic default.+  * ''locationArrayNames'' //<wrap lo>(String or Array)</wrap>// - //Optional// - Either a string of either '' 'customer'', '' 'shipping'' or '' 'both'', or an array of both like ''['customer''shipping']''. If not specified, '' 'both'' is the automatic default.
  
 **Example:**  **Example:** 
-  * ''FC.locations.removeCountries("US");'' - Removes the United States from both the billing and the shipping country location fields +  * ''FC.locations.removeCountries('US');'' - Removes the United States from both the billing and the shipping country location fields 
-  * ''FC.locations.removeCountries(["CA""GB"], "shipping");'' - Removes Canada and the United Kingdom from the shipping location fields+  * ''FC.locations.removeCountries(['CA''GB'], 'shipping');'' - Removes Canada and the United Kingdom from the shipping location fields
  
 ---- ----
Line 199: Line 215:
 **Parameters:**  **Parameters:** 
   * ''countries'' //<wrap lo>(String or Array)</wrap>// - Either a string or an array of strings of the 2 character ISO country codes   * ''countries'' //<wrap lo>(String or Array)</wrap>// - Either a string or an array of strings of the 2 character ISO country codes
-  * ''locationArrayNames'' //<wrap lo>(String or Array)</wrap>// - //Optional// - Either a string of either ''"customer"'', ''"shipping"'' or ''"both"'', or an array of both like ''["customer""shipping"]''. If not specified, ''"both"'' is the automatic default.+  * ''locationArrayNames'' //<wrap lo>(String or Array)</wrap>// - //Optional// - Either a string of either '' 'customer'', '' 'shipping'' or '' 'both'', or an array of both like ''['customer''shipping']''. If not specified, '' 'both'' is the automatic default.
  
 **Example:**  **Example:** 
-  * ''FC.locations.limitCountriesTo("AU");'' - Allows only Australia to be selected in the billing and shipping country location fields +  * ''FC.locations.limitCountriesTo('AU');'' - Allows only Australia to be selected in the billing and shipping country location fields 
-  * ''FC.locations.limitCountriesTo(["US""CA"], "shipping");'' - Allows only the United States and Canada to be selected for the shipping country+  * ''FC.locations.limitCountriesTo(['US''CA'], 'shipping');'' - Allows only the United States and Canada to be selected for the shipping country
  
 ---- ----
Line 213: Line 229:
   * ''country'' //<wrap lo>(String)</wrap>// - A string of the 2 character ISO country codes   * ''country'' //<wrap lo>(String)</wrap>// - A string of the 2 character ISO country codes
   * ''states'' //<wrap lo>(String or Array)</wrap>// - Either a string or an array of strings of the ISO state codes    * ''states'' //<wrap lo>(String or Array)</wrap>// - Either a string or an array of strings of the ISO state codes 
-  * ''locationArrayNames'' //<wrap lo>(String or Array)</wrap>// - //Optional// - Either a string of either ''"customer"'', ''"shipping"'' or ''"both"'', or an array of both like ''["customer""shipping"]''. If not specified, ''"both"'' is the automatic default.+  * ''locationArrayNames'' //<wrap lo>(String or Array)</wrap>// - //Optional// - Either a string of either '' 'customer'', '' 'shipping'' or '' 'both'', or an array of both like ''['customer''shipping']''. If not specified, '' 'both'' is the automatic default.
  
 **Example:**  **Example:** 
-  * ''FC.locations.removeStates("US""AK");'' - Prevents Alaska from being selected as a state when the United States is selected +  * ''FC.locations.removeStates('US''AK');'' - Prevents Alaska from being selected as a state when the United States is selected 
-  * ''FC.locations.removeStates("US", ["AK""HI"], "shipping");'' - Prevents Alaska and Hawaii from being selected as a state for shipping when the United States is selected as the shipping country+  * ''FC.locations.removeStates('US', ['AK''HI'], 'shipping');'' - Prevents Alaska and Hawaii from being selected as a state for shipping when the United States is selected as the shipping country
  
 ---- ----
Line 228: Line 244:
   * ''country'' //<wrap lo>(String)</wrap>// - A string of the 2 character ISO country codes   * ''country'' //<wrap lo>(String)</wrap>// - A string of the 2 character ISO country codes
   * ''states'' //<wrap lo>(String or Array)</wrap>// - Either a string or an array of strings of the ISO state codes    * ''states'' //<wrap lo>(String or Array)</wrap>// - Either a string or an array of strings of the ISO state codes 
-  * ''locationArrayNames'' //<wrap lo>(String or Array)</wrap>// - //Optional// - Either a string of either ''"customer"'', ''"shipping"'' or ''"both"'', or an array of both like ''["customer""shipping"]''. If not specified, ''"both"'' is the automatic default.+  * ''locationArrayNames'' //<wrap lo>(String or Array)</wrap>// - //Optional// - Either a string of either '' 'customer'', '' 'shipping'' or '' 'both'', or an array of both like ''['customer''shipping']''. If not specified, '' 'both'' is the automatic default.
  
 **Example:**  **Example:** 
-  * ''FC.locations.limitStatesTo("US""CA");'' - Allows only California to be selected as a state when the United States is selected +  * ''FC.locations.limitStatesTo('US''CA');'' - Allows only California to be selected as a state when the United States is selected 
-  * ''FC.locations.limitStatesTo("AU", ["SA""VIC""QLD"], "shipping");'' - Allows only South Australia, Victoria and Queensland from being selected for shipping when Australia is selected as the shipping country.+  * ''FC.locations.limitStatesTo('AU', ['SA''VIC''QLD'], 'shipping');'' - Allows only South Australia, Victoria and Queensland from being selected for shipping when Australia is selected as the shipping country.
  
 ---- ----
Line 256: 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>FC.locations.limitCountriesTo("US""shipping");+<code javascript>FC.locations.limitCountriesTo('US''shipping');
 FC.checkout.requireShippingAddress();</code> FC.checkout.requireShippingAddress();</code>
  
Line 264: 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>FC.locations.limitCountriesTo(["US""CA"]); +<code javascript>FC.locations.limitCountriesTo(['US''CA']); 
-FC.locations.removeStates("US", ["AK""HI"]);</code>+FC.locations.removeStates('US', ['AK''HI']);</code> 
 + 
 + 
 +=== Example 3 === 
 + 
 +  - Remove all non-standard US States 
 + 
 +<code javascript>FC.locations.removeStates('US', ['AF','AA','AC','AE','AM','AP','AS','PR','VI','GU','FM','MH','MP'], 'both');</code> 
 + 
 + 
 + 
 +=== Example 4 === 
 + 
 +  - 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>FC.locations.removeCountries(['CU','IR','SD','KP']);</code>
  
 ==== What about making it read-only? ==== ==== What about making it read-only? ====
Line 275: Line 305:
  
   * 2012/05/09 - v1.0 - Initial version   * 2012/05/09 - v1.0 - Initial version
 +  * 2013/03/19 - v1.1 - Changed ''updateFoxyComplete()'' to prevent ''validateLocationName()'' from calling ''updateShipping()'', to prevent the repetition of shipping results displayed

Site Tools