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
v:2.0:snippets:dynamic_product_total_with_multicurrency_support [2019/12/01 21:42] – [Step 1: Include snippet] adamv:2.0:snippets:dynamic_product_total_with_multicurrency_support [2023/08/15 06:36] (current) – [Step 3: Configuration for currency conversion] adam
Line 19: Line 19:
 <code javascript> <code javascript>
 <script> <script>
-/* Foxy Dynamic Product Prices and Currency Conversion v0.3, https://foxy.io */ +/* Foxy Dynamic Product Prices and Currency Conversion v0.8, https://foxy.io */ 
-(function(c,b){var g=function(c){return new y(c)};g.version="0.1.3";var k=c.fxSetup||{rates:{},base:""};g.rates=k.rates;g.base=k.base;g.settings={from:k.from||g.base,to:k.to||g.base};var t=g.convert=function(c,b){if("object"===typeof c&&c.length){for(var d=0;d<c.length;d++)c[d]=t(c[d],b);return c}b=b||{};b.from||(b.from=g.settings.from);b.to||(b.to=g.settings.to);d=b.to;var z=b.from,l=g.rates;l[g.base]=1;if(!l[d]||!l[z])throw"fx error";d=z===g.base?l[d]:d===g.base?1/l[z]:1/l[z]*l[d];return c*d},y= +!function(t,e){var n=function(t){return new i(t)};n.version="0.1.3";var a=t.fxSetup||{rates:{},base:""};n.rates=a.rates,n.base=a.base,n.settings={from:a.from||n.base,to:a.to||n.base};var r=n.convert=function(t,e){if("object"==typeof t&&t.length){for(var a=0;a<t.length;a++)t[a]=r(t[a],e);return t}(e=e||{}).from||(e.from=n.settings.from),e.to||(e.to=n.settings.to);a=e.to;var i=e.from,s=n.rates;if(s[n.base]=1,!s[a]||!s[i])throw"fx error";return t*(a=i===n.base?s[a]:a===n.base?1/s[i]:s[a]*(1/s[i]))},i=function(t){"string"==typeof t?(this._v=parseFloat(t.replace(/[^0-9-.]/g,"")),this._fx=t.replace(/([^A-Za-z])/g,"")):this._v=t};(a=n.prototype=i.prototype).convert=function(){var t=Array.prototype.slice.call(arguments);return t.unshift(this._v),r.apply(n,t)},a.from=function(t){return(t=n(r(this._v,{from:t,to:n.base})))._fx=n.base,t},a.to=function(t){return r(this._v,{from:this._fx?this._fx:n.settings.from,to:t})},"undefined"!=typeof exports?("undefined"!=typeof module&&module.exports&&(exports=module.exports=n),exports.fx=fx):"function"==typeof define&&define.amd?define([],(function(){return n})):(n.noConflict=function(e){return function(){return t.fx=e,n.noConflict=undefined,n}}(t.fx),t.fx=n)}(this),function(t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).numberCurrencyFormat=t()}((function(){return function t(e,n,a){function r(s,o){if(!n[s]){if(!e[s]){var c="function"==typeof require&&require;if(!o&&c)return c(s,!0);if(i)return i(s,!0);var u=new Error("Cannot find module '"+s+"'");throw u.code="MODULE_NOT_FOUND",u}var f=n[s]={exports:{}};e[s][0].call(f.exports,(function(t){return r(e[s][1][t]||t)}),f,f.exports,t,e,n,a)}return n[s].exports}for(var i="function"==typeof require&&require,s=0;s<a.length;s++)r(a[s]);return r}({1:[function(t,e,n){"use strict";const a="LEFT",r="ALWAYS",i="NEVER",s="IF_NEEDED",o="AS_IS",c={currency:"",showDecimals:r,thousandSeparator:",",decimalSeparator:".",currencyPosition:"RIGHT",decimalsDigits:2,spacing:!0,arithmeticalRounding:!1};function u(t,e){return void 0!==t?t:e}function f(t){return t?{currency:t.currency||c.currency,thousandSeparator:u(t.thousandSeparator,c.thousandSeparator),decimalSeparator:u(t.decimalSeparator,c.decimalSeparator),showDecimals:t.showDecimals||c.showDecimals,decimalsDigits:u(t.decimalsDigits,c.decimalsDigits),currencyPosition:t.currencyPosition||c.currencyPosition,spacing:u(t.spacing,c.spacing),arithmeticalRounding:u(t.arithmeticalRounding,c.arithmeticalRounding)}:c}function l(t){return/^[\.,]$/.test(t)}e.exports={format:function(t,e){const n=f(e),c=n.showDecimals===o?t.toString():parseFloat((n.arithmeticalRounding?function(t,e){return+(Math.round(t+"e+"+e)+"e-"+e)}(t,n.decimalsDigits):+t).toString()).toFixed(n.showDecimals===i?0:n.decimalsDigits);if(isNaN(c))return t;const u=c.split("."),l=u[0],d=u[1]||"",p=n.showDecimals===r||n.showDecimals===s&&+d>0||n.showDecimals===o&&d.length>0?n.decimalSeparator+d:"",h=(m=l,g=n.thousandSeparator,m.split("").reduce((function(t,e,n){return t+e+(n<m.length-1&&(m.length-n-1)%3==0&&"-"!==e?g:"")}),"")+p);var m,g;if(n.currency){const t=n.spacing?" ":"";return n.currencyPosition===a?n.currency+t+h:h+t+n.currency}return h},unformat:function(t,e){const n=f(e),a=e&&e.decimalSeparator,r=t.replace(/\s/g,"").match(/[0-9]+|[^0-9]+/gi).map((function(t,e){return{value:t,isNumber:/[0-9]+/.test(t),index:e}})),i=r.filter((function(t){return t.isNumber})),s=r.filter((function(t){return!t.isNumber})),o=i[0],c=i[i.length-1],u=o.value>0?o:i.length>1&&i[1].value>0?i[1]:void 0;if(!u)return 0;const d=u.index>0?r[u.index-1]:void 0,p=d&&1===s.length&&l(d.value),h=c.index-1,m=h>=0&&r[h],g=s.filter((function(t){return t.index>o.index&&t.index<h})).map((function(t){return t.value})),_=g.length>0&&g.indexOf(m.value)<0,y=m&&m.value===a,v=c.value.length===n.decimalsDigits&&m&&l(m.value),x=(a?y:v||y||p||_)?c.value.length:0,b=d&&d.value,j=b&&"-"===b[b.length-1],w=i.map((function(t){return t.value})).join("");return parseFloat(w/Math.pow(10,x))*(j?-1:1)}}},{}]},{},[1])(1)}));var FC=FC||{};!function(t,e){var n={version:.8},r=/([{\|])p([+\-:])([\d\.]+)(?:\D{3})?([\|}])/,i=/^(\d+):/,s=!1,o={base:"USD",sets:[],max_rate_cache:12,rounding:"minimal",convert_display_only:!1,active_class:"active",use_openexchangerates:!1,adjust_price_display:null};function c(){for(var t=e.json.shipping_address.country,n=""==e.json.template_set?"DEFAULT":e.json.template_set,a="DEFAULT",r=0;r<settings.sets.length;r++)settings.sets[r].hasOwnProperty("countries")&&settings.sets[r].countries.indexOf(t)>-1&&(a=settings.sets[r].template_set);F()||(O(),a==n)?(m(),u()):h(a)}function u(n){var a=settings.sets.map((function(t,e){return t.currency})).filter((function(t,e,n){return n.indexOf(t)===e})),r=(n=void 0!==n&&n,F());r.hasOwnProperty("rates")&&(Date.now()-r.rates.timestamp)/60<36e4*parseFloat(settings.max_rate_cache)&&(n||r.fcsid==e.json.session_id)?(fx.rates=r.rates.rates,fx.base=r.rates.base,d()):n||(settings.use_openexchangerates?t.getJSON("https://openexchangerates.org/api/latest.json?app_id="+settings.use_openexchangerates+"&prettyprint=false&symbols="+a.join(","),(function(t){l(t.rates,t.base),d()})):t.getJSON("https://www.floatrates.com/daily/"+settings.base+".json",(function(t){for(var e={},n=0;n<a.length;n++)(t.hasOwnProperty(a[n].toLowerCase())||a[n]==settings.base)&&(e[a[n]]=a[n]==settings.base?1:t[a[n].toLowerCase()].rate);l(e,settings.base),d()})))}function l(t,e){fx.rates=t,fx.base=e,O({rates:t,base:e,timestamp:Date.now()})}function d(){var n=F();if(!n)return console.log("FC.currency: Unable to find locale information"),!1;var a=D(),i=e.json.item_count>0?e.json.locale_info.int_curr_symbol.trim():n.locale.int_curr_symbol.trim(),s=e.json.item_count>0?e.json.locale_info.mon_decimal_point:n.locale.mon_decimal_point;t(".fc-currency-price").each((function(){var a,r=settings.base;t(this).data("fc-currency")&&(r=t(this).data("fc-currency"),a=s);var o=S(t(this).html(),a),c=o;r!=i&&(c=g(c=fx.convert(o,{from:r,to:i}))),t(this).html(e.util.money_format(n.currency_format,_(c)).trim()).data("fc-currency",i).show()})),t('form[action*="'+a+'"]').each((function(){var e=t(this).find('[name="price"], [name$=":price"]');if(0==e.length)return!1;var n=settings.base;if(t(this).data("fc-currency"))n=t(this).data("fc-currency");else{var a=/([\d\.]+)([A-Z]{3})$/i,s=e.first().val().replace(" ","");s.match(a);a.test(s)&&(n=a.exec(s)[2].toUpperCase())}n!=i&&(t(this).find("[value*='{p'], [value*='|p']").each((function(){var t=this.value.match(r);if(t){var e=fx.convert(parseFloat(t[3]),{from:n,to:i});e=g(e),settings.convert_display_only||(this.value=this.value.replace(r,"$1p$2"+e.toFixed(2)+i.toLowerCase()+"$4"))}})),e.each((function(){var t=S(this.value),e=fx.convert(t,{from:n,to:i});e=g(e),settings.convert_display_only||(this.value=e.toFixed(2)+i.toLowerCase())})),t(this).data("fc-currency",i)),y()}))}function h(t){e.client.request("https://"+e.json.config.store_domain+"/cart?template_set="+t).done((function(){O(),c()}))}function m(){for(var n=F(),a=!1,r=e.json.item_count>0?""==e.json.template_set?"DEFAULT":e.json.template_set:n.template_set,i=0;i<settings.sets.length;i++)settings.sets[i].template_set==r&&(a=r);a&&(t("[data-fc-template-set]."+settings.active_class).removeClass(settings.active_class),t('[data-fc-template-set="'+a+'"]').addClass(settings.active_class))}function g(t){var e=["full","half","minimal","none",!1].indexOf(settings.rounding)>-1?settings.rounding:o.rounding;if("full"==e)return Math.ceil(t);if("half"==e){var n=t%1;return Math.floor(t)+(0==n?0:n<=.5?.5:1)}return"minimal"==e?Math.ceil(20*t)/20:t}function _(t){return"function"==typeof settings.adjust_price_display&&(adjusted_price=settings.adjust_price_display(t),void 0!==adjusted_price)?parseFloat(adjusted_price):t}function y(){ADJUST={},t("input,select").off("change.foxy-dynamic-price"),t('form[action*="'+D()+'"]').each((function(){var e=t(this),n="",a={products:{}};t(this).find("[name='name'],[name^='name||'],[name$=':name'],[name*=':name||']").each((function(){var t=b(this.name),r=t?t+":":"",i=S(e.find("[name='"+r+"price'],[name^='"+r+"price||']").first().val()),s={id:t,code:"",base_price:i||0,quantity:1,attributes:{},has_quantity:!1},o=e.find("[name='"+r+"quantity'],[name^='"+r+"quantity||']"),c=e.find("[name='"+r+"code'],[name^='"+r+"code||']");if(c.length>0&&(s.code=v(c.first().val()),""===n&&(n=s.code)),o.length>0){var u=0,f=j(o);["select","text","hidden"].indexOf(f)>-1?(s.has_quantity=!0,u=parseFloat(v(o.val()))):["radio","checkbox"].indexOf(f)>-1&&(s.has_quantity=!0,1==o.filter(":checked").length&&(u=parseFloat(v(o.filter(":checked").val())))),isNaN(u)&&(u=0),s.quantity=u}a.products[t]=s}));e.attr("data-fc-form-code")&&(n=e.attr("data-fc-form-code")),""!==n&&(t(this).find("input,select").each((function(){var e=b(this.name),i=function(t){return x(t)[2]}(this.name),s=j(t(this));if("quantity"!=i)if("price"!=i||"hidden"==s){if("SELECT"==this.tagName){var o=!1;t(this).children("option").each((function(){this.value.search(r)>-1&&(o=!0)})),o&&(t(this).data("fc-adjust-for",n),a.products[e].attributes[v(this.name)]=v(this.value),t(this).on("change.foxy-dynamic-price",(function(){ADJUST[t(this).data("fc-adjust-for")].products[e].attributes[v(this.name)]=v(this.value),w()})))}else if(this.value.search(r)>-1)switch(t(this).data("fc-adjust-for",n),t(this).attr("type")){case"checkbox":t(this).is(":checked")?a.products[e].attributes[v(this.name)]=v(this.value):a.products[e].attributes[v(this.name)]="",t(this).on("change.foxy-dynamic-price",(function(){t(this).is(":checked")?ADJUST[t(this).data("fc-adjust-for")].products[e].attributes[v(this.name)]=v(this.value):ADJUST[t(this).data("fc-adjust-for")].products[e].attributes[v(this.name)]="",w()}));break;case"radio":a.products[e].attributes.hasOwnProperty(v(this.name))||(a.products[e].attributes[v(this.name)]=""),t(this).is(":checked")&&(a.products[e].attributes[v(this.name)]=v(this.value)),t("[name='"+this.name+"']").data("fc-adjust-for",n).on("change.foxy-dynamic-price",(function(){ADJUST[t(this).data("fc-adjust-for")].products[e].attributes[v(this.name)]=v(this.value),w()}))}}else t(this).data("fc-adjust-for",n).on("change.foxy-dynamic-price",(function(){var n=0;(["select","text"].indexOf(s)>-1||["radio","checkbox"].indexOf(s)>-1&&t(this).is(":checked"))&&(n=parseFloat(v(this.value))),isNaN(n)&&(n=0),ADJUST[t(this).data("fc-adjust-for")].products[e].base_price=n,w()}));else t(this).data("fc-adjust-for",n).on("change.foxy-dynamic-price",(function(){var n=0;(["select","text"].indexOf(s)>-1||["radio","checkbox"].indexOf(s)>-1&&t(this).is(":checked"))&&(n=parseFloat(v(this.value))),isNaN(n)&&(n=0),ADJUST[t(this).data("fc-adjust-for")].products[e].quantity=n,w()}))})),ADJUST[n]=a)})),n._ADJUST=ADJUST,w()}function v(t){return t.replace(/\|\|[\d\w]+(?:\|\|open)?$/,"")}function x(t){return(t=v(t)).match(/(?:(\d+):)?(.*)/)}function b(t){var e=x(t);return i.test(this.name)&&(prefix=parseInt(this.name.match(i)[0])),void 0===e[1]?0:parseInt(e[1])}function j(t){if("SELECT"==t[0].tagName)return"select";if("INPUT"==t[0].tagName)switch(t.attr("type").toLowerCase()){case"text":case"number":case"tel":return"text";default:return t.attr("type").toLowerCase()}}function w(){for(f in ADJUST){var n=0,i=0;for(p in ADJUST[f].products){var s=ADJUST[f].products[p].base_price,o=0;for(a in ADJUST[f].products[p].attributes){var c=ADJUST[f].products[p].attributes[a].match(r);if(c)switch(c[2]){case":":s=parseFloat(c[3]);break;case"+":o+=parseFloat(c[3]);break;case"-":o-=parseFloat(c[3])}}s+=o,n+=s*=ADJUST[f].products[p].quantity,i+=ADJUST[f].products[p].quantity}"function"==typeof fcFormatPrice&&(n=fcFormatPrice(n,f)),"function"==typeof fcFormatQuantity&&(i=fcFormatQuantity(i,f));var u=F(),l=e.util.money_format(u.currency_format,_(n)).trim();t("."+f+"_total, ."+f+"-total").html(l),t("."+f+"_total_quantity, ."+f+"-total-quantity").html(i)}}function D(){var t=F();return e.json.hasOwnProperty("config")?e.json.config.store_domain:t?t.store_domain:".foxycart.com"}function S(t,e){try{var n={};return e&&(n.decimalSeparator=e),numberCurrencyFormat.unformat(t,n)}catch(t){return!1}}function O(t){var n=F(),a=Object.assign({},n);n&&""==e.json.template_set&&void 0!==t||Object.assign(a,{fcsid:e.json.session_id,store_domain:e.json.config.store_domain,template_set:""==e.json.template_set?"DEFAULT":e.json.template_set,locale:e.json.locale_info,currency_format:e.json.config.currency_format}),"object"==typeof t&&Object.assign(a,{rates:t}),localStorage.setItem("fc_currency_config",JSON.stringify(a))}function F(){var t=JSON.parse(localStorage.getItem("fc_currency_config"));return!(null===t||e.hasOwnProperty("json")&&e.json.hasOwnProperty("session_id")&&t.fcsid!==e.json.session_id)&&t}e.hasOwnProperty("util")||(e.util={},e.util.money_format=function(t,n){if("number"!=typeof n)return null;var a=e.json.locale_info;return t.replace(/%((=.|[+^(!-])*?)(\d*?)(#(\d+))?(\.(\d+))?([in%])/g,(function(t,r,i,s,o,c,u,f,l){if(t=u="","%"===l)return"%";u=r&&/=./.test(r)?r.match(/=(.)/)[1]:" ",o=!r||-1===r.indexOf("!"),s=parseInt(s,10)||0,(i=0>n)&&(n=-n),f=""===f||void 0===f?"i"===l?a.int_frac_digits:a.frac_digits:parseInt(f,10);var d=(n=(n=e.util.round(n,f)).toFixed(f)).indexOf(".");f=-1!==d?n.slice(0,d):n,d=-1!==d?n.slice(d+1):"";var p=function(t,e,n){return(t=t.split("")).splice(e,0,n),t.join("")},h=f.length,m=h<(c=parseInt(c,10));if(m){var g=c-h;f=Array(g+1).join(u)+f}if(-1===r.indexOf("^")){if(c=a.mon_thousands_sep,(h=a.mon_grouping)[0]<f.length)for(var _=0,y=f.length;_<h.length&&!(0>=(y-=h[_]));_++)m&&y<g&&(c=u),f=p(f,y,c);if(0<h[_-1])for(;y>h[_-1];)y-=h[_-1],m&&y<g&&(c=u),f=p(f,y,c)}if(u=d?f+a.mon_decimal_point+d:f,g="",o&&(g="i"===l?a.int_curr_symbol:a.currency_symbol),l=i?a.n_sign_posn:a.p_sign_posn,_=i?a.n_sep_by_space:a.p_sep_by_space,y=i?a.n_cs_precedes:a.p_cs_precedes,-1!==r.indexOf("("))t=(y?g+(1===_?" ":""):"")+u+(y?"":(1===_?" ":"")+g),t=i?"("+t+")":" "+t+" ";else switch(c=a.positive_sign,o=a.negative_sign,f=i?o:c,d="",l&&(d=Array((i?c:o).length-f.length+1).join(" ")),l){case 0:t="("+(y?g+(1===_?" ":"")+u:u+(1===_?" ":"")+g)+")";break;case 1:t=d+f+(2===_?" ":"")+(y?g+(1===_?" ":"")+u:u+(1===_?" ":"")+g);break;case 2:t=(y?g+(1===_?" ":"")+u:u+(1===_?" ":"")+g)+(2===_?" ":"")+f+d;break;case 3:t=y?d+f+(2===_?" ":"")+g+(1===_?" ":"")+u:u+(1===_?" ":"")+f+d+(2===_?" ":"")+g;break;case 4:t=y?g+(2===_?" ":"")+d+f+(1===_?" ":"")+u:u+(1===_?" ":"")+g+(2===_?" ":"")+f+d}return 0<(s-=t.length)&&(s=Array(s+1).join(" "),t=-1!==r.indexOf("-")?t+s:s+t),t}))},e.util.round=function(t,e){var n=Math.pow(10,e);return 5<=10*t*n%10&&(t+=1/(10*n)),Math.round(t*n)/n}),n.update=function(){settings=e.hasOwnProperty("currency_config")?Object.assign({},o,e.currency_config):Object.assign({},o),s=e.hasOwnProperty("session")&&!0===e.session.initialized;var t=F();!t||e.hasOwnProperty("json")&&0!=e.json.item_count||(e.hasOwnProperty("json")||(e.json={}),e.json.currency_format=t.currency_format,e.json.locale_info=t.locale,e.json.item_count=0),(t&&!s||s)&&(settings.sets.length>1?s?c():(m(),u(!0)):(t||O(),y()))};var A="function"==typeof e.onLoad?e.onLoad:function(){};e.onLoad=function(){e.client.on("ready.done",n.update),A()},t((function(){n.update(),t("body").on("click","[data-fc-template-set]",(function(e){var n=function(t){for(var e=!1,n=0;n<settings.sets.length;n++)settings.sets[n].template_set==t&&(e=settings.sets[n]);return e}(t(this).attr("data-fc-template-set"));n&&(e.preventDefault(),s&&h(n.template_set))}))})),e.currency=n}(jQuery,FC);
-function(c){"string"===typeof c?(this._v=parseFloat(c.replace(/[^0-9-.]/g,"")),this._fx=c.replace(/([^A-Za-z])/g,"")):this._v=c};k=g.prototype=y.prototype;k.convert=function(){var c=Array.prototype.slice.call(arguments);c.unshift(this._v);return t.apply(g,c)};k.from=function(c){c=g(t(this._v,{from:c,to:g.base}));c._fx=g.base;return c};k.to=function(c){return t(this._v,{from:this._fx?this._fx:g.settings.from,to:c})};"undefined"!==typeof exports?("undefined"!==typeof module&&module.exports&&(exports= +
-module.exports=g),exports.fx=fx):"function"===typeof define&&define.amd?define([],function(){return g}):(g.noConflict=function(d){return function(){c.fx=d;g.noConflict=b;return g}}(c.fx),c.fx=g)})(this)+
-!function(c){"object"==typeof exports&&"undefined"!=typeof module?module.exports=c():"function"==typeof define&&define.amd?define([],c):("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).numberCurrencyFormat=c()}(function(){return function t(b,g,k){function y(m,z){if(!g[m]){if(!b[m]){var l="function"==typeof require&&require;if(!z&&l)return l(m,!0);if(d)return d(m,!0);l=Error("Cannot find module '"+m+"'");throw l.code="MODULE_NOT_FOUND",l;}l=g[m]= +
-{exports:{}};b[m][0].call(l.exports,function(h){return y(b[m][1][h]||h)},l,l.exports,t,b,g,k)}return g[m].exports}for(var d="function"==typeof require&&require,m=0;m<k.length;m++)y(k[m]);return y}({1:[function(b,g,k){function t(b,g){return void 0!==b?b:g}function y(b){return b?{currency:b.currency||d.currency,thousandSeparator:t(b.thousandSeparator,d.thousandSeparator),decimalSeparator:t(b.decimalSeparator,d.decimalSeparator),showDecimals:b.showDecimals||d.showDecimals,decimalsDigits:t(b.decimalsDigits, +
-d.decimalsDigits),currencyPosition:b.currencyPosition||d.currencyPosition,spacing:t(b.spacing,d.spacing),arithmeticalRounding:t(b.arithmeticalRounding,d.arithmeticalRounding)}:d}var d={currency:"",showDecimals:"ALWAYS",thousandSeparator:",",decimalSeparator:".",currencyPosition:"RIGHT",decimalsDigits:2,spacing:!0,arithmeticalRounding:!1};g.exports={format:function(b,g){var d=y(g),l="AS_IS"===d.showDecimals?b.toString():parseFloat((d.arithmeticalRounding?function(b,d){return+(Math.round(b+"e+"+d)+ +
-"e-"+d)}(b,d.decimalsDigits):+b).toString()).toFixed("NEVER"===d.showDecimals?0:d.decimalsDigits);if(isNaN(l))return b;l=l.split(".");var h=l[1]||"";h="ALWAYS"===d.showDecimals||"IF_NEEDED"===d.showDecimals&&0<+h||"AS_IS"===d.showDecimals&&0<h.length?d.decimalSeparator+h:"";l=(m=l[0],k=d.thousandSeparator,m.split("").reduce(function(b,d,h){return b+d+(h<m.length-1&&0==(m.length-h-1)%3&&"-"!==d?k:"")},"")+h);var m,k;return d.currency?(h=d.spacing?" ":"","LEFT"===d.currencyPosition?d.currency+h+l:l+ +
-h+d.currency):l},unformat:function(b,d){var g=y(d),l=d&&d.decimalSeparator,h=b.replace(/\s/g,"").match(/[0-9]+|[^0-9]+/gi).map(function(b,d){return{value:b,isNumber:/[0-9]+/.test(b),index:d}}),m=h.filter(function(b){return b.isNumber}),k=h.filter(function(b){return!b.isNumber}),t=m[0],r=m[m.length-1],A=0<t.value?t:1<m.length&&0<m[1].value?m[1]:void 0;if(!A)return 0;var E=(A=0<A.index?h[A.index-1]:void 0)&&1===k.length&&/^[\.,]$/.test(A.value),B=r.index-1;h=0<=B&&h[B];k=k.filter(function(b){return b.index> +
-t.index&&b.index<B}).map(function(b){return b.value});k=0<k.length&&0>k.indexOf(h.value);var v=h&&h.value===l;g=r.value.length===g.decimalsDigits&&h&&/^[\.,]$/.test(h.value);l=(l?v:g||v||E||k)?r.value.length:0;r=(r=A&&A.value)&&"-"===r[r.length-1];m=m.map(function(b){return b.value}).join("");return parseFloat(m/Math.pow(10,l))*(r?-1:1)}}},{}]},{},[1])(1)});var FC=FC||{}; +
-(function(c,b){function g(){for(var e=b.json.shipping_address.country,c=""==b.json.template_set?"DEFAULT":b.json.template_set,n="DEFAULT",F=0;F<settings.sets.length;F++)settings.sets[F].hasOwnProperty("countries")&&-1<settings.sets[F].countries.indexOf(e)&&(n=settings.sets[F].template_set);if(!v()&&(B(),n!=c)){m(n);return}H();t()}function k(){return settings.sets.map(function(b,c){return b.currency}).filter(function(b,c,n){return n.indexOf(b)===c})}function t(e){var q=k();e=void 0===e?!1:e;var n+
-v();n.hasOwnProperty("rates")&&(Date.now()-n.rates.timestamp)/60<36E4*parseFloat(settings.max_rate_cache)&&(e||n.rates.fcsid==b.json.session_id)?(fx.rates=n.rates.rates,fx.base=n.rates.base,d()):e||(settings.use_openexchangerates?c.getJSON("https://openexchangerates.org/api/latest.json?app_id="+settings.use_openexchangerates+"&prettyprint=false&symbols="+q.join(","),function(b){y(b.rates,b.base);d()}):c.getJSON("http://www.floatrates.com/daily/"+settings.base+".json",function(b){for(var c={},e=0;e< +
-q.length;e++)if(b.hasOwnProperty(q[e].toLowerCase())||q[e]==settings.base)c[q[e]]=q[e]==settings.base?1:b[q[e].toLowerCase()].rate;y(c,settings.base);d()}))}function y(b,c){fx.rates=b;fx.base=c;B({rates:b,base:c,timestamp:Date.now()})}function d(){var e=v();if(!e)return console.log("FC.currency: Unable to find locale information"),!1;var q=A(),n=0<b.json.item_count?b.json.locale_info.int_curr_symbol.trim():e.locale.int_curr_symbol.trim();c(".fc-currency-price").each(function(){var q=E(c(this).html()), +
-d=c(this).data("fc-currency")?c(this).data("fc-currency"):settings.base,h=q;d!=n&&(h=fx.convert(q,{from:d,to:n}),h=z(h));c(this).html(b.util.money_format(e.currency_format,h).trim()).data("fc-currency",n).show()});c('form[action*="'+q+'"]').each(function(){var b=c(this).find('[name="price"], [name$=":price"]');if(0==b.length)return!1;var e=settings.base;if(c(this).data("fc-currency"))e=c(this).data("fc-currency");else{var q=/([\d\.]+)([A-Z]{3})$/i,d=b.first().val().replace(" ","");d.match(q);q.test(d)&& +
-(e=q.exec(d)[2].toUpperCase())}e!=n&&(c(this).find("[value*='{p'], [value*='|p']").each(function(){var b=this.value.match(D);b&&(b=fx.convert(parseFloat(b[3]),{from:e,to:n}),b=z(b),settings.convert_display_only||(this.value=this.value.replace(D,"$1p$2"+b.toFixed(2)+n.toLowerCase()+"$4")))}),b.each(function(){var b=E(this.value);b=fx.convert(b,{from:e,to:n});b=z(b);settings.convert_display_only||(this.value=b.toFixed(2)+n.toLowerCase())}),c(this).data("fc-currency",n));l()})}function m(e){b.client.request("https://"+ +
-b.json.config.store_domain+"/cart?template_set="+e).done(function(){B();g()})}function H(){var e=v(),q=!1;e=0<b.json.item_count?""==b.json.template_set?"DEFAULT":b.json.template_set:e.template_set;for(var n=0;n<settings.sets.length;n++)settings.sets[n].template_set==e&&(q=e);q&&(c("[data-fc-template-set]."+settings.active_class).removeClass(settings.active_class),c('[data-fc-template-set="'+q+'"]').addClass(settings.active_class))}function z(b){var c=-1<["full","half","minimal","none",!1].indexOf(settings.rounding)? +
-settings.rounding:G.rounding;return"full"==c?Math.ceil(b):"half"==c?(c=b%1,Math.floor(b)+(0==c?0:.5>=c?.5:1)):"minimal"==c?Math.ceil(20*b)/20:b}function l(){ADJUST={};c("input,select").off("change.foxy-dynamic-price");c('form[action*="'+A()+'"]').each(function(){var b=c(this),d="",n={products:{}};c(this).find("[name='name'],[name^='name||'],[name$=':name'],[name*=':name||']").each(function(){var c=K(this.name),e=c?c+":":"",q=E(b.find("[name='"+e+"price'],[name^='"+e+"price||']").first().val());q= +
-{id:c,code:"",base_price:q?q:0,quantity:1,attributes:{},has_quantity:!1};var g=b.find("[name='"+e+"quantity'],[name^='"+e+"quantity||']");e=b.find("[name='"+e+"code'],[name^='"+e+"code||']");0<e.length&&(q.code=h(e.first().val()),""===d&&(d=q.code));if(0<g.length){e=0;var l=I(g);-1<["select","text"].indexOf(l)?(q.has_quantity=!0,e=parseFloat(h(g.val()))):-1<["radio","checkbox"].indexOf(l)&&(q.has_quantity=!0,1==g.filter(":checked").length&&(e=parseFloat(h(g.filter(":checked").val()))));isNaN(e)&& +
-(e=0);q.quantity=e}n.products[c]=q});b.attr("data-fc-form-code")&&(d=b.attr("data-fc-form-code"));""!==d&&(c(this).find("input,select").each(function(){var b=K(this.name),e=J(this.name)[2],q=I(c(this));if("quantity"==e)c(this).data("fc-adjust-for",d).on("change.foxy-dynamic-price",function(){var e=0;if(-1<["select","text"].indexOf(q)||-1<["radio","checkbox"].indexOf(q)&&c(this).is(":checked"))e=parseFloat(h(this.value));isNaN(e)&&(e=0);ADJUST[c(this).data("fc-adjust-for")].products[b].quantity=e; +
-r()});else if("price"==e&&"hidden"!=q)c(this).data("fc-adjust-for",d).on("change.foxy-dynamic-price",function(){var e=0;if(-1<["select","text"].indexOf(q)||-1<["radio","checkbox"].indexOf(q)&&c(this).is(":checked"))e=parseFloat(h(this.value));isNaN(e)&&(e=0);ADJUST[c(this).data("fc-adjust-for")].products[b].base_price=e;r()});else if("SELECT"==this.tagName){var g=!1;c(this).children("option").each(function(){-1<this.value.search(D)&&(g=!0)});g&&(c(this).data("fc-adjust-for",d),n.products[b].attributes[h(this.name)]= +
-h(this.value),c(this).on("change.foxy-dynamic-price",function(){ADJUST[c(this).data("fc-adjust-for")].products[b].attributes[h(this.name)]=h(this.value);r()}))}else if(-1<this.value.search(D))switch(c(this).data("fc-adjust-for",d),c(this).attr("type")){case "checkbox":c(this).is(":checked")?n.products[b].attributes[h(this.name)]=h(this.value):n.products[b].attributes[h(this.name)]="";c(this).on("change.foxy-dynamic-price",function(){c(this).is(":checked")?ADJUST[c(this).data("fc-adjust-for")].products[b].attributes[h(this.name)]= +
-h(this.value):ADJUST[c(this).data("fc-adjust-for")].products[b].attributes[h(this.name)]="";r()});break;case "radio":n.products[b].attributes.hasOwnProperty(h(this.name))||(n.products[b].attributes[h(this.name)]=""),c(this).is(":checked")&&(n.products[b].attributes[h(this.name)]=h(this.value)),c("[name='"+this.name+"']").data("fc-adjust-for",d).on("change.foxy-dynamic-price",function(){ADJUST[c(this).data("fc-adjust-for")].products[b].attributes[h(this.name)]=h(this.value);r()})}}),ADJUST[d]=n)}); +
-C._ADJUST=ADJUST;r()}function h(b){return b.replace(/\|\|[\d\w]+(?:\|\|open)?$/,"")}function J(b){b=h(b);return b.match(/(?:(\d+):)?(.*)/)}function K(b){b=J(b);L.test(this.name)&&(prefix=parseInt(this.name.match(L)[0]));return void 0===b[1]?0:parseInt(b[1])}function I(b){if("SELECT"==b[0].tagName)return"select";if("INPUT"==b[0].tagName)switch(b.attr("type").toLowerCase()){case "text":case "number":case "tel":return"text";default:return b.attr("type").toLowerCase()}}function r(){for(f in ADJUST){var e= +
-0,d=0;for(p in ADJUST[f].products){var n=ADJUST[f].products[p].base_price,h=0;for(a in ADJUST[f].products[p].attributes){var g=ADJUST[f].products[p].attributes[a].match(D);if(g)switch(g[2]){case ":":n=parseFloat(g[3]);break;case "+":h+=parseFloat(g[3]);break;case "-":h-=parseFloat(g[3])}}n+=h;n*=ADJUST[f].products[p].quantity;e+=n;d+=ADJUST[f].products[p].quantity}"function"===typeof fcFormatPrice&&(e=fcFormatPrice(e,f));"function"===typeof fcFormatQuantity&&(d=fcFormatQuantity(d,f));n=v();e=b.util.money_format(n.currency_format, +
-e).trim();c("."+f+"_total, ."+f+"-total").html(e);c("."+f+"_total_quantity, ."+f+"-total-quantity").html(d)}}function A(){var c=v();return b.json.hasOwnProperty("config")?b.json.config.store_domain:c?c.store_domain:".foxycart.com"}function E(b){try{return numberCurrencyFormat.unformat(b)}catch(q){return!1}}function B(c){var e=v(),d=Object.assign({},e);e&&""==b.json.template_set&&"undefined"!==typeof c||Object.assign(d,{fcsid:b.json.session_id,store_domain:b.json.config.store_domain,template_set:""== +
-b.json.template_set?"DEFAULT":b.json.template_set,locale:b.json.locale_info,currency_format:b.json.config.currency_format});"object"===typeof c&&Object.assign(d,{rates:c});localStorage.setItem("fc_currency_config",JSON.stringify(d))}function v(){var c=JSON.parse(localStorage.getItem("fc_currency_config"));return null===c||b.hasOwnProperty("json")&&b.json.hasOwnProperty("session_id")&&c.fcsid!==b.json.session_id?!1:c}var C={version:.3},D=/([{\|])p([+\-:])([\d\.]+)(?:\D{3})?([\|}])/,L=/^(\d+):/,G={base:"USD", +
-sets:[],max_rate_cache:12,rounding:"minimal",convert_display_only:!1,active_class:"active",use_openexchangerates:!1};b.hasOwnProperty("util")||(b.util={},b.util.money_format=function(c,d){if("number"!==typeof d)return null;var e=b.json.locale_info;return c.replace(/%((=.|[+^(!-])*?)(\d*?)(#(\d+))?(\.(\d+))?([in%])/g,function(c,g,h,q,n,l,m,k,t){c=m="";if("%"===t)return"%";m=g&&/=./.test(g)?g.match(/=(.)/)[1]:" ";n=!g||-1===g.indexOf("!");q=parseInt(q,10)||0;(h=0>d)&&(d=-d);k=""===k||void 0===k?"i"=== +
-t?e.int_frac_digits:e.frac_digits:parseInt(k,10);d=b.util.round(d,k);d=d.toFixed(k);var r=d.indexOf(".");k=-1!==r?d.slice(0,r):d;r=-1!==r?d.slice(r+1):"";var y=function(b,c,d){b=b.split("");b.splice(c,0,d);return b.join("")},v=k.length;l=parseInt(l,10);var z=v<l;if(z){var w=l-v;k=Array(w+1).join(m)+k}if(-1===g.indexOf("^")){l=e.mon_thousands_sep;v=e.mon_grouping;if(v[0]<k.length)for(var u=0,x=k.length;u<v.length;u++){x-=v[u];if(0>=x)break;z&&x<w&&(l=m);k=y(k,x,l)}if(0<v[u-1])for(;x>v[u-1];)x-=v[u- +
-1],z&&x<w&&(l=m),k=y(k,x,l)}m=r?k+e.mon_decimal_point+r:k;w="";n&&(w="i"===t?e.int_curr_symbol:e.currency_symbol);t=h?e.n_sign_posn:e.p_sign_posn;u=h?e.n_sep_by_space:e.p_sep_by_space;x=h?e.n_cs_precedes:e.p_cs_precedes;if(-1!==g.indexOf("("))c=(x?w+(1===u?" ":""):"")+m+(x?"":(1===u?"\u00a0":"")+w),c=h?"("+c+")":"\u00a0"+c+"\u00a0";else switch(l=e.positive_sign,n=e.negative_sign,k=h?n:l,r="",t&&(r=Array((h?l:n).length-k.length+1).join(" ")),t){case 0:c="("+(x?w+(1===u?"\u00a0":"")+m:m+(1===u?"\u00a0": +
-"")+w)+")";break;case 1:c=r+k+(2===u?"\u00a0":"")+(x?w+(1===u?"\u00a0":"")+m:m+(1===u?"\u00a0":"")+w);break;case 2:c=(x?w+(1===u?"\u00a0":"")+m:m+(1===u?"\u00a0":"")+w)+(2===u?"\u00a0":"")+k+r;break;case 3:c=x?r+k+(2===u?"\u00a0":"")+w+(1===u?"\u00a0":"")+m:m+(1===u?"\u00a0":"")+k+r+(2===u?"\u00a0":"")+w;break;case 4:c=x?w+(2===u?"\u00a0":"")+r+k+(1===u?"\u00a0":"")+m:m+(1===u?"\u00a0":"")+w+(2===u?"\u00a0":"")+k+r}q-=c.length;0<q&&(q=Array(q+1).join(" "),c=-1!==g.indexOf("-")?c+q:q+c);return c})}, +
-b.util.round=function(b,c){var d=Math.pow(10,c);5<=10*b*d%10&&(b+=1/(10*d));return Math.round(b*d)/d});C.update=function(){settings=b.hasOwnProperty("currency_config")?Object.assign({},G,b.currency_config):Object.assign({},G);var c=b.hasOwnProperty("session")&&!0===b.session.initialized,d=v();!d||b.hasOwnProperty("json")&&0!=b.json.item_count||(b.hasOwnProperty("json")||(b.json={}),b.json.currency_format=d.currency_format,b.json.locale_info=d.locale,b.json.item_count=0);if(d&&!c||c)1<settings.sets.length? +
-c?g():(H(),t(!0)):(d||B(),l())};var M="function"==typeof b.onLoad?b.onLoad:function(){};b.onLoad=function(){b.client.on("ready.done",C.update);M()};c(function(){C.update();c("body").on("click","[data-fc-template-set]",function(b){var d=c(this).attr("data-fc-template-set");for(var e=!1,g=0;g<settings.sets.length;g++)settings.sets[g].template_set==d&&(e=settings.sets[g]);if(d=e)b.preventDefault(),m(d.template_set)})});b.currency=C})(jQuery,FC);+
 </script> </script>
 </code> </code>
Line 119: Line 89:
   : **Accepts:** String   : **Accepts:** String
   : **Description:** Set to the API key for your OpenExchangeRates account if you would prefer to use it instead of the default https://floatrates.com source for currency rates.   : **Description:** Set to the API key for your OpenExchangeRates account if you would prefer to use it instead of the default https://floatrates.com source for currency rates.
 +  ; ''adjust_price_display''
 +  : **Accepts:** Function
 +  : **Description:** Create a function to modify only the price displayed on the page, for example to make a tax exclusive price in your add to cart to be displayed as tax inclusive to the customer. The function is passed a single argument for the price to be modified, and the function must return the modified price as well.
  
 <WRAP center round tip 95%> <WRAP center round tip 95%>
Line 157: Line 130:
 </code> </code>
  
-If a customer from Germany or Belgium visits your website, it will automatically apply the template set from your store with a code of ''de'', and then convert the prices to all be in Euro. If someone visits from Canada, then it will update to the ''ca'' template set, and change the prices to Canadian dollars.+If a customer from Germany or Belgium visits your website, it will automatically apply the template set from your store with a code of ''Germany'', and then convert the prices to all be in Euro. If someone visits from Canada, then it will update to the ''Canada'' template set, and change the prices to Canadian dollars.
  
 If curreny conversion is enabled for the snippet, then any ''price'' and ''p'' product option modifiers will be dynamically converted to the current currency of the cart, rounding as specified by the ''rounding'' configuration option. If curreny conversion is enabled for the snippet, then any ''price'' and ''p'' product option modifiers will be dynamically converted to the current currency of the cart, rounding as specified by the ''rounding'' configuration option.
Line 183: Line 156:
     'rounding': 'half',     'rounding': 'half',
     'active_class': 'currency-option-active',     'active_class': 'currency-option-active',
-    'use_openexchangerates': 'abc123abc123abc123'+    'use_openexchangerates': 'abc123abc123abc123'
 +    'adjust_price_display': function(price) { return price * 1.25; }
 }; };
 </script> </script>
 </code> </code>
 +
 +<WRAP center round important 95%>
 +**Note:** The above is simply an example of what it looks like with all attributes for the configuration option. You will not want to copy this to use directly in your set up, but rather review each configuration option and only add them if you actually need them.
 +</WRAP>
 +
  
 <WRAP center round info 95%>By default this snippet will pull the currency conversion rates from [[https://floatrates.com|floatrates.com]]. It does also support [[https://openexchangerates.org/|Open Exchange Rates]] (which is also what we use for our own default currency conversion in the cart), but you'll need to create your own account and specify your API key using the ''use_openexchangerates'' configuration option.</WRAP> <WRAP center round info 95%>By default this snippet will pull the currency conversion rates from [[https://floatrates.com|floatrates.com]]. It does also support [[https://openexchangerates.org/|Open Exchange Rates]] (which is also what we use for our own default currency conversion in the cart), but you'll need to create your own account and specify your API key using the ''use_openexchangerates'' configuration option.</WRAP>

Site Tools