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 %}
@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