Skip to content

Instantly share code, notes, and snippets.

@sadiesaurus
Forked from carolineschnapp/currencies.liquid
Last active November 9, 2017 15:55
Show Gist options
  • Save sadiesaurus/1b682cd6a1ae9bf5e689140fe0f3ac7a to your computer and use it in GitHub Desktop.
Save sadiesaurus/1b682cd6a1ae9bf5e689140fe0f3ac7a 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 }}
<script>
window.onload = function() {
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;
}
}
/* For currency drop-down menu */
// 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);
/* For currency button */
// Select all your currencies buttons.
var buttons = jQuery('#currencies span');
// When customer clicks on a currency button.
buttons.click(function() {
buttons.removeClass('selected');
jQuery(this).addClass('selected');
var newCurrency = jQuery(this).attr('data-currency');
Currency.convertAll(Currency.currentCurrency, newCurrency);
});
// For options.
var original_selectCallback = window.selectCallback;
var selectCallback = function(variant, selector) {
original_selectCallback(variant, selector);
Currency.convertAll(shopCurrency, jQuery('#currencies span.selected').attr('data-currency'));
jQuery('.selected-currency').text(Currency.currentCurrency);
};
$('body').on('ajaxCart.afterCartLoad', function(cart) {
Currency.convertAll(shopCurrency, jQuery('#currencies span.selected').attr('data-currency'));
jQuery('.selected-currency').text(Currency.currentCurrency);
});
jQuery('.selected-currency').text(Currency.currentCurrency);
}
</script>
{% endif %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment