Change currency depending on visitor location

Hello,
I have a website and im trying to add a function where we can redirect visitors to see products in the currency of their location.

I have already added “Wix Currency converter” and it if we change the currency from the dropdown menue it adds extension to the path “?currency={currency}” and {currency} can be EUR,USD or whatever currency we added.

So i created this function to get the location of the vistor and redirecting him to the relevant currency depending on his location:

import wixLocation from ‘wix-location’ ;
$w . onReady ( function () {

// Fetching the user's location details 
fetch ( 'https://extreme-ip-lookup.com/json/?key={API_TOKEN}' , { 
        method :  'get' 
    }) 
    // Check if the request was successful 
    . then (( httpResponse ) => { 
        if  ( httpResponse . ok ) { 
            **return**  httpResponse . json (); 
        } 
    }) 
    . then (( json ) => { 
        // Set the user's countryCode as a const 
        let  loc  =  json . countryCode ; 

        if  ( loc  ===  "FR" ) { 
            let  currentPage  =  wixLocation . url ; 
            let  url  =  currentPage  +  '?currency=EUR' 
            wixLocation . to ( url ); 
        } 
    }) 

});

my problem is when i put this function in masterPage.js or any of the website pages, it does an infinit loop. which means it works at first but then it keeps adding the extension at the end of the website

so when a user visits the website instead of him getting redirected to " https://www.website.com/?currency=EUR " it will keep looping and adding the extension at the end like " https://www.website.com/?currency=EUR?currency=EUR?currency=EUR… "

I also tried instead of adding the extension dynamically i tried to put it as

wixLocation . to (" https://www.website.com/?currency=EUR" );

but it keeps refreshing which means its still in infinit loop.

Thanks for your help in advance :slight_smile:

2 Likes

Your loop is coming from this code…

let currentPage=wixLocation.url;
let url=currentPage+ '?currency=EUR'   
wixLocation.to(url);

What you are doing here is grabbing the url that the visitor is currently sitting on, adding a parameter and then reloading the exact same page now with a parameter which is triggering the code to run again.

You will want to use this. queryParams - Velo API Reference - Wix.com

1 Like

Thanks for your reply. I actually fixed the code and added couple countries. i will put the code here for anyone who wants to reuse it (need to replace {API_TOKEN} with your token from extreme-api-lookup).

you can put the code in head part in custom code in order for the code to run before the page is rendered. Also you need to have a premium website in order to add the wix currency converter (change the currency converter element to hidden as the currency will be converted automatically).

I hope it helps :slight_smile:

I am glad this is working for you, however, I would still recommend to anyone else looking that you utilize the Velo apis for modifying query parameters
https://www.wix.com/velo/reference/wix-location/queryparams

Glad you have a working solution

Hi, is this still working for you?

Any chance you can provide more specific guidance. I understand i want to be adding a query parameter

// 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

Hello SgtBatten, saifmohanad1! Is this still working for you? If yes, is it possible help me out to set it up here as well, it is really vital for my type of busines and went through all topics possible without any success so far…

Yeah it is. Just get an API key for yourself from the site and then paste the entire code block with your API key inserted.

1 Like

Hello SgtBatten! You really made my day by replying :slightly_smiling_face:, I thought nobody would reply by the previous activity!

I am not really a programer and am not really sure where to paste the code sicne I don’t want to break something. I have an API ket from extreme-ip-lookup.com now! Can you guide me how to set it up, I would really appreciate it! You can contact me on my email as well : info@danellys-shop.com

For all the others wondering, open WIX Editor → on the left where your menu is, the last one is called “Code” with the following symbol { } . Click on it, then click “Start codng” and paste the code provided by SgtBatten with your API key inserted.

@SgtBatten I found out that the script does not work when entering the domain from FB ads for example, do you have the same issue?