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
Next revisionBoth sides next revision
v:2.0:snippets:locations_modfication [2019/12/09 05:40] – [Step 1: Add Javascript] adamv:2.0:snippets:locations_modfication [2020/02/20 06:25] – [Step 1: Add Javascript] adam
Line 29: Line 29:
     };     };
  
-    /* Country/State Helper Functions v2.*/+    /* Country/State Helper Functions v2.*/
     FC.locations.removeCountries=function(a,b){if("undefined"==typeof a)return!1;"string"==typeof a&&(a=[a]);b=FC.locations.validateLocationArrayNames(b);for(l in b){0===FC.json.config["locations_"+b[l]].length&&(FC.json.config["locations_"+b[l]]={});for(var c in a)FC.json.config["locations_"+b[l]][a[c]]="*"}return!0};     FC.locations.removeCountries=function(a,b){if("undefined"==typeof a)return!1;"string"==typeof a&&(a=[a]);b=FC.locations.validateLocationArrayNames(b);for(l in b){0===FC.json.config["locations_"+b[l]].length&&(FC.json.config["locations_"+b[l]]={});for(var c in a)FC.json.config["locations_"+b[l]][a[c]]="*"}return!0};
     FC.locations.limitCountriesTo=function(a,b){if("undefined"==typeof a)return!1;"string"==typeof a&&(a=[a]);b=FC.locations.validateLocationArrayNames(b);for(l in b){var c=FC.json.config.locations;FC.json.config["locations_"+b[l]]={};for(var d in c)-1==a.indexOf(d)&&(FC.json.config["locations_"+b[l]][d]="*")}return!0};     FC.locations.limitCountriesTo=function(a,b){if("undefined"==typeof a)return!1;"string"==typeof a&&(a=[a]);b=FC.locations.validateLocationArrayNames(b);for(l in b){var c=FC.json.config.locations;FC.json.config["locations_"+b[l]]={};for(var d in c)-1==a.indexOf(d)&&(FC.json.config["locations_"+b[l]][d]="*")}return!0};
     FC.locations.removeStates=function(a,b,c){if("undefined"==typeof a||"undefined"==typeof b)return!1;"string"==typeof b&&(b=[b]);c=FC.locations.validateLocationArrayNames(c);for(l in c){if("undefined"==typeof FC.json.config.locations[a])return!1;FC.json.config["locations_"+c[l]].hasOwnProperty(a)||(FC.json.config["locations_"+c[l]][a]=[]);for(var d in b)FC.json.config["locations_"+c[l]][a].push(b[d])}return!0};     FC.locations.removeStates=function(a,b,c){if("undefined"==typeof a||"undefined"==typeof b)return!1;"string"==typeof b&&(b=[b]);c=FC.locations.validateLocationArrayNames(c);for(l in c){if("undefined"==typeof FC.json.config.locations[a])return!1;FC.json.config["locations_"+c[l]].hasOwnProperty(a)||(FC.json.config["locations_"+c[l]][a]=[]);for(var d in b)FC.json.config["locations_"+c[l]][a].push(b[d])}return!0};
     FC.locations.limitStatesTo=function(a,b,c){if("undefined"==typeof a||"undefined"==typeof b)return!1;"string"==typeof b&&(b=[b]);c=FC.locations.validateLocationArrayNames(c);for(l in c){var d=FC.json.config.locations;if("undefined"==typeof d[a])return!1;FC.json.config["locations_"+c[l]][a]=[];for(var e in d[a].r.options)-1==jQuery.inArray(e,b)&&FC.json.config["locations_"+c[l]][a].push(e)}return!0};     FC.locations.limitStatesTo=function(a,b,c){if("undefined"==typeof a||"undefined"==typeof b)return!1;"string"==typeof b&&(b=[b]);c=FC.locations.validateLocationArrayNames(c);for(l in c){var d=FC.json.config.locations;if("undefined"==typeof d[a])return!1;FC.json.config["locations_"+c[l]][a]=[];for(var e in d[a].r.options)-1==jQuery.inArray(e,b)&&FC.json.config["locations_"+c[l]][a].push(e)}return!0};
-    FC.locations.validateLocationArrayNames=function(a){if("undefined"==typeof a||""==a||"both"==a)a=["billing","shipping"];"string"==typeof a&&(a=[a]);for(var b=0;b<a.length;b++)"customer"==a[b]&&(a[b]="billing");return a};FC.locations.reset=function(a){a=FC.locations.validateLocationArrayNames(a);for(l in a)FC.json.config["locations_"+a[l]]={};customLocationsHandler()}; +    FC.locations.validateLocationArrayNames=function(a){if("undefined"==typeof a||""==a||"both"==a)a=["billing","shipping"];"string"==typeof a&&(a=[a]);for(var b=0;b<a.length;b++)"customer"==a[b]&&(a[b]="billing");return a};FC.locations.reset=function(a){a=FC.locations.validateLocationArrayNames(a);for(l in a)FC.json.config["locations_"+a[l]]=FC.locations.hasOwnProperty("locations_"+a[l]+"_default")?FC.locations["locations_"+a[l]+"_default"]:{};customLocationsExecute()}; 
-    function customLocationsHandler(){FC.locations.logic();FC.Template(FC.json.context).clearOutput();"cart"==FC.json.context?FC.cart.renderAddressEntry():(FC.checkout.renderCustomerBilling(),FC.checkout.renderCustomerShipping())};+    function customLocationsHandler(){FC.locations.hasOwnProperty("locations_billing_default")?FC.locations.reset():(FC.locations.locations_billing_default=FC.json.config.locations_billing,FC.locations.locations_shipping_default=FC.json.config.locations_shipping,customLocationsExecute())} 
 +    function customLocationsExecute(){FC.locations.logic();FC.Template(FC.json.context).clearOutput();"cart"==FC.json.context?FC.cart.renderAddressEntry():(FC.checkout.renderCustomerBilling(),FC.checkout.renderCustomerShipping())};
     {% if cart_is_fullpage or context == 'checkout' %}FC.client.on('ready', customLocationsHandler);{% else %}customLocationsHandler();{% endif %}     {% if cart_is_fullpage or context == 'checkout' %}FC.client.on('ready', customLocationsHandler);{% else %}customLocationsHandler();{% endif %}
 })(jQuery, FC); })(jQuery, FC);
Line 48: Line 49:
 Removes the specified countries from the specified locations arrays Removes the specified countries from the specified locations arrays
  
-**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 '' 'billing' '', '' 'shipping' '' or '' 'both' '', or an array of both like ''['billing', 'shipping']''. If not specified, '' 'both' '' is the automatic default.   * ''locationArrayNames'' //<wrap lo>(String or Array)</wrap>// - //Optional// - Either a string of either '' 'billing' '', '' 'shipping' '' or '' 'both' '', or an array of both like ''['billing', '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 61: Line 62:
 Restricts the specified locations arrays to just the countries specified Restricts the specified locations arrays to just the countries specified
  
-**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 '' 'billing' '', '' 'shipping' '' or '' 'both' '', or an array of both like ''['billing', 'shipping']''. If not specified, '' 'both' '' is the automatic default.   * ''locationArrayNames'' //<wrap lo>(String or Array)</wrap>// - //Optional// - Either a string of either '' 'billing' '', '' 'shipping' '' or '' 'both' '', or an array of both like ''['billing', '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 74: Line 75:
 Removes the specified states from the specified locations arrays for the specified country Removes the specified states from the specified locations arrays for the specified country
  
-**Parameters:** +**Parameters:**
   * ''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 '' 'billing' '', '' 'shipping' '' or '' 'both' '', or an array of both like ''['billing', 'shipping']''. If not specified, '' 'both' '' is the automatic default.   * ''locationArrayNames'' //<wrap lo>(String or Array)</wrap>// - //Optional// - Either a string of either '' 'billing' '', '' 'shipping' '' or '' 'both' '', or an array of both like ''['billing', '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 89: Line 90:
 Restricts the states for the specified country to only those specified Restricts the states for the specified country to only those specified
  
-**Parameters:** +**Parameters:**
   * ''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 '' 'billing' '', '' 'shipping' '' or '' 'both' '', or an array of both like ''['billing', 'shipping']''. If not specified, '' 'both' '' is the automatic default.   * ''locationArrayNames'' //<wrap lo>(String or Array)</wrap>// - //Optional// - Either a string of either '' 'billing' '', '' 'shipping' '' or '' 'both' '', or an array of both like ''['billing', '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.
 +
 +----
 +
 +
 +=== FC.locations.reset() ===
 +Resets the location restrictions back to the store default, as set in the "configuration" in the Foxy administration. Best used through the [[..:javascript|javascript events]] on the cart/checkout to trigger the reset based on events, such as removing an item from the cart.
 +
 +**Parameters:**
 +  * ''locationArrayNames'' //<wrap lo>(String or Array)</wrap>// - //Optional// - Either a string of either '' 'billing' '', '' 'shipping' '' or '' 'both' '', or an array of both like ''['billing', 'shipping']''. If not specified, '' 'both' '' is the automatic default.
 +
 +**Example:**
 +  * ''FC.locations.reset;'' - Reset the location restrictions back to default
 +  * ''FC.client.on('cart-item-remove.done', function() { FC.locations.reset(); });'' - Reset location restrictions whenever an item is removed from the cart
 +
  
  
-   
 ==== Examples ==== ==== Examples ====
  
Line 138: Line 152:
  
   * 2014/08/21 - v2.0 - Initial version   * 2014/08/21 - v2.0 - Initial version
 +  * 2019/11/09 - v2.1 - Adding ''reset()'' function

Site Tools