skip to Main Content

I am using this script for formatting money in Shopify. The problem is that I’d need the flexibility of having multiple currencies and the current function doesn’t seem to support it.

I’ve tried including that script in a .liquid file, and using {{ currency.symbol }} like: Shopify.money_format = "{{ currency.symbol }}" + "{{amount}}"; or "{{ currency.symbol }}{{amount}}"; or {{ currency.symbol }} + "{{amount}}"; but that gives an error with placeholderRegex which becomes undefined.

Thanks!

EDIT:
This is the code where I use the function:

Shopify.onItemAdded = function(line_item) {
    jQuery.getJSON('/cart.js', function(cart) {
       // change total price
       $('#popup_total').html(Shopify.formatMoney(cart.total_price))
    });
};

EDIT[2]: At the end I went with a different approach. I declared the current currency variable outside the function, and then I replace the original $ symbol, with whatever currency symbol the store has. It goes like this:

var currentCurrency = '{{ cart.currency.symbol }}'

Shopify.onItemAdded = function(line_item) {
    jQuery.getJSON('/cart.js', function(cart) {
         var total = Shopify.formatMoney(cart.total_price).replace('$', currentCurrency)
         $('#popup_total').html(total)
    })
}

3

Answers


  1. That function doesn’t limit you to a single currency – the first parameter is the unformatted amount, and the second is the desired format, which can have whichever currency you want – e.g. "€ {{amount}}"

    EDIT:

    It looks like you aren’t using the second format parameter. Could you try:

    Shopify.formatMoney(cart.total_price, "€{{amount}}")
    

    Or whatever currency you want there? Thanks for providing additional code, but it’s still difficult to understand the input and desired output, as well as other variables you have access to (you mentioned a dynamic currency, but where is that stored? Which variable?)

    Login or Signup to reply.
  2. default for Dollar

    Shopify.formatMoney(data.price) 
    

    For Pound sterling money format

    Shopify.formatMoney(data.price, "£{{amount}}")
    

    For Euro money format

    Shopify.formatMoney(data.price, "€{{amount}}") 
    
    Login or Signup to reply.
  3. Just try that

    let currencySymbol = {{ cart.currency.symbol }}
    

    and replace with

    Shopify.formatMoney(cart.total_price).replace('$', 'currencySymbol');
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search