Convert amounts to visitor currency

In this tutorial we demonstrate how to convert amounts and prices on the fly to each of your website visitor's currency based on their IP address with client side Javascript code :

The Core and Extended APIs provide automatic price conversions into your visitors currencies.

When you add a data-auto-convert-currencies attribute to the dbip.js script tag, it will automatically convert currencies for elements that have a dbip-convert-currency class :

Alternatively you can use the dbip.convertCurrencies() method to convert dynamically loaded amounts or provide on-demand conversion to your visitors :

dbip.convertCurrencies() can optionally take a DOM selector as parameter and returns a Promise object.


Advanced conversion options

When an amount has been converted, a converted class is added so you can use CSS to show or hide it as you see fit. The example below shows how to always display the base currency and only show the converted amount when necessary.

data-format attributes if present are used to customize the format of the displayed amount. The {amount} is replaced by the actual converted amount.

The data-currency-display attribute may be added to either the global dbip.js script tag or individial elements. It specifies how the currency is to be displayed and can be symbol ($), code (USD), or name (dollar).

The code example below is using a combination of those conversion options :



Compatibility

API DB
Free Basic Core Extended IP to Country IP to City IP to Location IP to ISP IP to Location+ISP

Was this article helpful ? Share it with others by clicking the social media buttons !