JS - auto currency switcher

moofawsaw
New Member
2 0 0

I have created a simple Shopify currency selector

 

{% form 'currency' %}
{{ form | currency_selector }}
{% endform %}

 

I want to use to allow the user to update the currency while also auto-detect the user's location and update the currency.

The current JS code works in that is auto-detects the user's location and updates the currency however it's stuck in a refresh loop when I call the change function from the JSON function. Does anyone know how to update the code so that the currency is automatically detected and updated in Shopify?  

 

window.addEventListener("DOMContentLoaded", function () {
  (function ($) {
    $(document).ready(function (e) {
      var currmap = {
        BD: "BDT",
        BE: "EUR",
        BF: "XOF",
        BG: "BGN",
        BA: "BAM",
        BB: "BBD",
        WF: "XPF",
        BL: "EUR",
        BM: "BMD",
        BN: "BND",
        BO: "BOB",
        BH: "BHD",
        BI: "BIF",
        BJ: "XOF",
        BT: "BTN",
        JM: "JMD",
        BV: "NOK",
        BW: "BWP",
        WS: "WST",
        BQ: "USD",
        BR: "BRL",
        BS: "BSD",
        JE: "GBP",
        BY: "BYR",
        BZ: "BZD",
        RU: "RUB",
        RW: "RWF",
        RS: "RSD",
        TL: "USD",
        RE: "EUR",
        TM: "TMT",
        TJ: "TJS",
        RO: "RON",
        TK: "NZD",
        GW: "XOF",
        GU: "USD",
        GT: "GTQ",
        GS: "GBP",
        GR: "EUR",
        GQ: "XAF",
        GP: "EUR",
        JP: "JPY",
        GY: "GYD",
        GG: "GBP",
        GF: "EUR",
        GE: "GEL",
        GD: "XCD",
        GB: "GBP",
        GA: "XAF",
        SV: "USD",
        GN: "GNF",
        GM: "GMD",
        GL: "DKK",
        GI: "GIP",
        GH: "GHS",
        OM: "OMR",
        TN: "TND",
        JO: "JOD",
        HR: "HRK",
        HT: "HTG",
        HU: "HUF",
        HK: "HKD",
        HN: "HNL",
        HM: "AUD",
        VE: "VEF",
        PR: "USD",
        PS: "ILS",
        PW: "USD",
        PT: "EUR",
        SJ: "NOK",
        PY: "PYG",
        IQ: "IQD",
        PA: "PAB",
        PF: "XPF",
        PG: "PGK",
        PE: "PEN",
        PK: "PKR",
        PH: "PHP",
        PN: "NZD",
        PL: "PLN",
        PM: "EUR",
        ZM: "ZMK",
        EH: "MAD",
        EE: "EUR",
        EG: "EGP",
        ZA: "ZAR",
        EC: "USD",
        IT: "EUR",
        VN: "VND",
        SB: "SBD",
        ET: "ETB",
        SO: "SOS",
        ZW: "ZWL",
        SA: "SAR",
        ES: "EUR",
        ER: "ERN",
        ME: "EUR",
        MD: "MDL",
        MG: "MGA",
        MF: "EUR",
        MA: "MAD",
        MC: "EUR",
        UZ: "UZS",
        MM: "MMK",
        ML: "XOF",
        MO: "MOP",
        MN: "MNT",
        MH: "USD",
        MK: "MKD",
        MU: "MUR",
        MT: "EUR",
        MW: "MWK",
        MV: "MVR",
        MQ: "EUR",
        MP: "USD",
        MS: "XCD",
        MR: "MRO",
        IM: "GBP",
        UG: "UGX",
        TZ: "TZS",
        MY: "MYR",
        MX: "MXN",
        IL: "ILS",
        FR: "EUR",
        IO: "USD",
        SH: "SHP",
        FI: "EUR",
        FJ: "FJD",
        FK: "FKP",
        FM: "USD",
        FO: "DKK",
        NI: "NIO",
        NL: "EUR",
        NO: "NOK",
        NA: "NAD",
        VU: "VUV",
        NC: "XPF",
        NE: "XOF",
        NF: "AUD",
        NG: "NGN",
        NZ: "NZD",
        NP: "NPR",
        NR: "AUD",
        NU: "NZD",
        CK: "NZD",
        XK: "EUR",
        CI: "XOF",
        CH: "CHF",
        CO: "COP",
        CN: "CNY",
        CM: "XAF",
        CL: "CLP",
        CC: "AUD",
        CA: "CAD",
        CG: "XAF",
        CF: "XAF",
        CD: "CDF",
        CZ: "CZK",
        CY: "EUR",
        CX: "AUD",
        CR: "CRC",
        CW: "ANG",
        CV: "CVE",
        CU: "CUP",
        SZ: "SZL",
        SY: "SYP",
        SX: "ANG",
        KG: "KGS",
        KE: "KES",
        SS: "SSP",
        SR: "SRD",
        KI: "AUD",
        KH: "KHR",
        KN: "XCD",
        KM: "KMF",
        ST: "STD",
        SK: "EUR",
        KR: "KRW",
        SI: "EUR",
        KP: "KPW",
        KW: "KWD",
        SN: "XOF",
        SM: "EUR",
        SL: "SLL",
        SC: "SCR",
        KZ: "KZT",
        KY: "KYD",
        SG: "SGD",
        SE: "SEK",
        SD: "SDG",
        DO: "DOP",
        DM: "XCD",
        DJ: "DJF",
        DK: "DKK",
        VG: "USD",
        DE: "EUR",
        YE: "YER",
        DZ: "DZD",
        US: "USD",
        UY: "UYU",
        YT: "EUR",
        UM: "USD",
        LB: "LBP",
        LC: "XCD",
        LA: "LAK",
        TV: "AUD",
        TW: "TWD",
        TT: "TTD",
        TR: "TRY",
        LK: "LKR",
        LI: "CHF",
        LV: "EUR",
        TO: "TOP",
        LT: "LTL",
        LU: "EUR",
        LR: "LRD",
        LS: "LSL",
        TH: "THB",
        TF: "EUR",
        TG: "XOF",
        TD: "XAF",
        TC: "USD",
        LY: "LYD",
        VA: "EUR",
        VC: "XCD",
        AE: "AED",
        AD: "EUR",
        AG: "XCD",
        AF: "AFN",
        AI: "XCD",
        VI: "USD",
        IS: "ISK",
        IR: "IRR",
        AM: "AMD",
        AL: "ALL",
        AO: "AOA",
        AQ: "",
        AS: "USD",
        AR: "ARS",
        AU: "AUD",
        AT: "EUR",
        AW: "AWG",
        IN: "INR",
        AX: "EUR",
        AZ: "AZN",
        IE: "EUR",
        ID: "IDR",
        UA: "UAH",
        QA: "QAR",
        MZ: "MZN"
      };
      $.getJSON("//freegeoip.app/json/", function (location) {
        if (location.country_code) {
          var mycurrency = currmap[location.country_code];
          if (mycurrency) {
            $("#currency_form select").val(mycurrency);
            $("#currency_form select").change();
          }
        }
      });
    });
  })(jQuery);
});

$("#currency_form select").on("change", function (e) {
  $("#currency_form").submit();
  console.log("cur change: ", $(this).val());
});

 

 

0 Likes
LamQSolutions
Pathfinder
115 27 40

Hi,

Could you share your store URL so we can take a look at the problem closer?

Found my answer helpful? Please LIKE or Accept Solutions.
You may be interested in our apps
Scrolly Telling creating scroll-based animations visually.
Awesome Product Description adding tabs and beautiful contents to product description.
0 Likes