Skip to content

Instantly share code, notes, and snippets.

@carolineschnapp
Last active February 20, 2023 23:19
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 8 You must be signed in to fork a gist
  • Save carolineschnapp/773624 to your computer and use it in GitHub Desktop.
Save carolineschnapp/773624 to your computer and use it in GitHub Desktop.
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 %}
@mattwheelr
Copy link

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

@imran8143
Copy link

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

@metalslug87
Copy link

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.

@vemundeldegard
Copy link

I am using cartJS, so I changed line 66 to:

$(document).on('cart.ready', function(event, cart) {
  Currency.convertAll(shopCurrency, jQuery('#currencies span.selected').attr('data-currency'));
  jQuery('.selected-currency').text(Currency.currentCurrency);
});
$(document).on('cart.requestComplete', function(event, cart) {
  Currency.convertAll(shopCurrency, jQuery('#currencies span.selected').attr('data-currency'));
  jQuery('.selected-currency').text(Currency.currentCurrency);
});

This works brilliantly. Until the customer decided to switch the currency back. Then the prices in my cartJS, turns to 0. What am I doing wrong?

@dirad
Copy link

dirad commented Feb 18, 2018

Hey @carolineschnapp Thank for this...

By using money_without_trailing_zeros or remove: '.00' on my products, the code breaks and gives wrong prices.. $0.17 instead of $17.54 for example.
Hoping for a tip =]

@squirrp
Copy link

squirrp commented Sep 6, 2018

I am using Simple theme, I have implemented everything - however whenever I touch variants, it all pings back to USD default??

@Mason-Senti
Copy link

Hi,
Nice to be with you, I use the Shopify platform "Theme SUPPLY"

I just installed the codes to convert the currency, as on the link:
https://help.shopify.com/en/themes/customization/currencies/show-multiple-currencies

for my site I use ( $ USD )
so I use the following settings:
         Settings / General /
              Timezone: (GMT -8: 00) Pacific Time (US & Canada)
              Unit system : Imperial System
              Default weight unit : Pound (Ib)

              Store currency : United States Dollars (USD)
       Change formatting:
               HTML with currency:
                         $ {{amount}} USD </ span>
               HTML without currency :
                          $ {{amount}} </ span>
              Email with currency :
                         $ {{amount}} USD
              Email without currency:
                         $ {{Amount}}
I followed all the indications to the letter except those which preceded

So here's what happens:
  I open my site, on the home page, I select a new currency ( £ )
all prices change according to the currency selected,
I click on one of my product, the product page opens with the dollar value Price initially save
when I go back to the currency selector ($ USD) the price flames
for example, from $ 40.00 to $62.00.
but when I click on home everything is back to normal.
Thanks for the help !

@Mason-Senti
Copy link

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.

Hi,
I use supply theme, and I have the same problem as you, do you find a solution?

@serhiosipatron
Copy link

serhiosipatron commented Oct 2, 2018

Hello @carolineschnapp, using debut, everything is great except one thing. When a product has different prices in my variants, my span.money is not called for updates, like setting 'data-currency', its just span.money without attr.

Only if I call change method with my select, I receive attr, but still function Currency.converAll is broken for this case

   $('#SingleOptionSelector-0').change(function(){
            setTimeout(function () {
               jQuery('span.money').each(function() {
                   jQuery(this).attr('data-currency-'+Currency.currentCurrency, jQuery(this).html());
                   console.log(jQuery(this).html()) // current variant price
               });
            }, 100 )
       });

@IlyaZha
Copy link

IlyaZha commented Dec 28, 2018

I have error "Currency.moneyFormats is undefined". When I set default value as empty object for it, I have new issue "Currency.cookie is undefined".
Upd: fixed it by adding jquery.

@yudechen0820
Copy link

yudechen0820 commented Jun 17, 2020

This is the code I have used in Shopify.
Currency.convertAll(window.shop_currency, jQuery('select[name=currencies] option:selected').val(), 'span.money', 'money_format');
But I am getting this error.
Uncaught TypeError: Currency.convertAll is not a function

@oknaus-huemor
Copy link

Interesting that if you use this file:
{{ "/services/javascripts/currencies.js" | script_tag }}

actually will pull different rates then what is used here:
{{ "//cdn.shopify.com/s/javascripts/currencies.js" | script_tag }}

I think the first one is more accurate.

@MrJonoCES
Copy link

Hey @carolineschnapp Thank for this...
By using money_without_trailing_zeros or remove: '.00' on my products, the code breaks and gives wrong prices.. $0.17 instead of $17.54 for example. Hoping for a tip =]

Did you find a solution to this error? I’m experiencing a similar thing when you change the quantity of an item in the cart it makes the price 100x smaller than it should be. If you refresh the page it displays correctly again, but any change in quantity makes the decimal place jump two places to the left

@dirad
Copy link

dirad commented Feb 20, 2023

Yeah i did, but it was a long time ago..
From what i can gather I did end up using {{ current_variant.price | money_without_trailing_zeros }}
hope this helps you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment