-
-
Save carolineschnapp/773624 to your computer and use it in GitHub Desktop.
{% 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 %} |
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'.
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:
- user enters the website first time
- user changes the currency from default USD to EURO
- user reloads the page
- the currency cookie remembers the previously choosen EURO value and automatically switches the prices to EURO
- 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.
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?
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 =]
I am using Simple theme, I have implemented everything - however whenever I touch variants, it all pings back to USD default??
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 !
Hi,
I think I found a bug in following Use Case:
- user enters the website first time
- user changes the currency from default USD to EURO
- user reloads the page
- the currency cookie remembers the previously choosen EURO value and automatically switches the prices to EURO
- 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?
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 )
});
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.
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
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.
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.
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.