Last active
December 2, 2024 05:18
-
-
Save carolineschnapp/773624 to your computer and use it in GitHub Desktop.
currencies.liquid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{% 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 %} |
Hey @carolineschnapp Thank for this...
By usingmoney_without_trailing_zeros
orremove: '.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
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
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.