Change currency depending on visitor location

// The code in this file will load on every page of your site

import wixLocationFrontend from 'wix-location-frontend';
import {fetch} from 'wix-fetch';

$w.onReady(function () {
	if ('currency' in wixLocationFrontend.query) {
      return;
    }

    fetch('https://extreme-ip-lookup.com/json/?key=YOURKEYHERE', {
      method: 'get'
    })
    .then((httpResponse) => {
      if (httpResponse.ok) {
        return httpResponse.json();
      }
    })
    .then((json) => {
      let loc = json.countryCode;

      let currencies = {
        'US': 'USD',
        'CA': 'CAD',
        'GB': 'GBP',
        'AU': 'AUD',
        'NZ': 'NZD',
      };

      let euroCountries = ['AT', 'BE', 'CY', 'EE', 'FI', 'FR', 'DE', 'GR', 'IE', 'IT', 'LV', 'LT', 'LU', 'MT', 'NL', 'PT', 'SK', 'SI', 'ES'];

      let currency;
      if (currencies[loc]) {
        currency = currencies[loc];
      } else if (euroCountries.includes(loc)) {
        currency = 'EUR';
      } else {
        return;
      }

      const key = 'currency';
      if (key && currency) {
        const toAdd = { [key]: currency };
        wixLocationFrontend.queryParams.add(toAdd);
      }
    })
});

For anyone still trying

2 Likes