Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
currencies.liquid
{% if settings.show_multiple_currencies %}
{{ "//cdn.shopify.com/s/javascripts/currencies.js" | script_tag }}
{{ "jquery.currencies.min.js" | asset_url | script_tag }}
<script>
Currency.format = '{{ settings.currency_format | default: 'money_with_currency_format' }}';
var shopCurrency = '{{ shop.currency }}';
/* Sometimes merchants change their shop currency, let's tell our JavaScript file */
Currency.moneyFormats[shopCurrency].money_with_currency_format = {{ shop.money_with_currency_format | strip_html | json }};
Currency.moneyFormats[shopCurrency].money_format = {{ shop.money_format | strip_html | json }};
/* Default currency */
var defaultCurrency = '{{ settings.default_currency | default: shop.currency }}';
/* Cookie currency */
var cookieCurrency = Currency.cookie.read();
/* Fix for customer account pages */
jQuery('span.money span.money').each(function() {
jQuery(this).parents('span.money').removeClass('money');
});
/* Saving the current price */
jQuery('span.money').each(function() {
jQuery(this).attr('data-currency-{{ shop.currency }}', jQuery(this).html());
});
// If there's no cookie.
if (cookieCurrency == null) {
if (shopCurrency !== defaultCurrency) {
Currency.convertAll(shopCurrency, defaultCurrency);
}
else {
Currency.currentCurrency = defaultCurrency;
}
}
// If the cookie value does not correspond to any value in the currency dropdown.
else if (jQuery('[name=currencies]').size() && jQuery('[name=currencies] option[value=' + cookieCurrency + ']').size() === 0) {
Currency.currentCurrency = shopCurrency;
Currency.cookie.write(shopCurrency);
}
else if (cookieCurrency === shopCurrency) {
Currency.currentCurrency = shopCurrency;
}
else {
Currency.convertAll(shopCurrency, cookieCurrency);
}
jQuery('[name=currencies]').val(Currency.currentCurrency).change(function() {
var newCurrency = jQuery(this).val();
Currency.convertAll(Currency.currentCurrency, newCurrency);
jQuery('.selected-currency').text(Currency.currentCurrency);
});
var original_selectCallback = window.selectCallback;
var selectCallback = function(variant, selector) {
original_selectCallback(variant, selector);
Currency.convertAll(shopCurrency, jQuery('[name=currencies]').val());
jQuery('.selected-currency').text(Currency.currentCurrency);
};
$('body').on('ajaxCart.afterCartLoad', function(cart) {
Currency.convertAll(shopCurrency, jQuery('[name=currencies]').val());
jQuery('.selected-currency').text(Currency.currentCurrency);
});
jQuery('.selected-currency').text(Currency.currentCurrency);
</script>
{% endif %}
Owner

carolineschnapp commented May 17, 2012

On line 42, is when you reload page when changing from any-currency-other-than-shop-currency back to shop currency. To avoid rounding errors.

Owner

carolineschnapp commented Aug 5, 2012

There's no more any need to reload the page when converting to the shop's currency. The currency conversion code has been refactored on August 5th to handle conversions 'lost in translation'.

stcqu commented Aug 22, 2013

I had trouble with this script running before my page was entirely loaded. This led to some currencies being untranslated. I was able to fix this by running the script on the document load event.

I had a similar problem as squebe when using the "Simple" Shopify theme - most elements were translated, but not all.

I resolved it by putting this on line 7:

$(document).ready(function() {

and closing the function by putting this on line 69:

});

Hopefully that's clear enough for any non-coders to apply.

Hi There,

above stuartellis [[[$(document).ready(function()]]] solution works well in all browser, but Safari.

In Product Page, I find the product currency will jump back to the default currency after the page is loaded (you will see the price jump in 1s). it only happens on safari (MAC), all other browsers do not have this issue. it would be great if anyone can help me out here. Big thanks

Here is a link from my client:
http://appleseedkid.com/products/camping-camouflage-hoodies-jacket

Hi There, Im trying to figure out if is possible to round the second currency and resting the value of 1.

Example, where USD is the default currency and COP my informative currency.

I would like to show this result $234.589 COP to $235.000 COP and resting 1 or 10 would be $234.999 COP or $234.990 COP

I Appreciate information! Thank you!

Hey there! Newb question. Is there a way to change the users cookie settings using a url parameter for this e.g ?cur=USD

Thanks,
Matt

Hi i am getting a issue with code when Change Currency it did not change instantly i have to click on cart icon and then it change currency
please help its urgent

Hi,

I think I found a bug in following Use Case:

  1. user enters the website first time
  2. user changes the currency from default USD to EURO
  3. user reloads the page
  4. the currency cookie remembers the previously choosen EURO value and automatically switches the prices to EURO
  5. user wants to switch the prices back to the USD but he can't, if he tries to switch to default USD value the prices won't
    change, they will remain the Euro. Even if he changes the currency to for example GBP for a moment and then tries to click on USD the prices will switch back to Euro instead of USD.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment