skip to Main Content

I found this Gist for money formatting on Shopify using JavaScript https://gist.github.com/stewartknapman/8d8733ea58d2314c373e94114472d44c

I placed it in my cart page and when I try:

Shopify.formatMoney(2000, '$')

I get this:

cart:2166 Uncaught TypeError: Cannot read property '1' of null
    at Object.Shopify.formatMoney (cart:2166)
    at <anonymous>:1:9

this is at switch(formatString.match(placeholderRegex)[1]) {

I expect to get $20.00

Do you know where the problem is?

Similar question: Shopify Buy Button Error: "cannot read property '1' of null"

The Gist content

var Shopify = Shopify || {};
// ---------------------------------------------------------------------------
// Money format handler
// ---------------------------------------------------------------------------
Shopify.money_format = "${{amount}}";
Shopify.formatMoney = function(cents, format) {
  if (typeof cents == 'string') { cents = cents.replace('.',''); }
  var value = '';
  var placeholderRegex = /{{s*(w+)s*}}/;
  var formatString = (format || this.money_format);

  function defaultOption(opt, def) {
     return (typeof opt == 'undefined' ? def : opt);
  }

  function formatWithDelimiters(number, precision, thousands, decimal) {
    precision = defaultOption(precision, 2);
    thousands = defaultOption(thousands, ',');
    decimal   = defaultOption(decimal, '.');

    if (isNaN(number) || number == null) { return 0; }

    number = (number/100.0).toFixed(precision);

    var parts   = number.split('.'),
        dollars = parts[0].replace(/(d)(?=(ddd)+(?!d))/g, '$1' + thousands),
        cents   = parts[1] ? (decimal + parts[1]) : '';

    return dollars + cents;
  }

  switch(formatString.match(placeholderRegex)[1]) {
    case 'amount':
      value = formatWithDelimiters(cents, 2);
      break;
    case 'amount_no_decimals':
      value = formatWithDelimiters(cents, 0);
      break;
    case 'amount_with_comma_separator':
      value = formatWithDelimiters(cents, 2, '.', ',');
      break;
    case 'amount_no_decimals_with_comma_separator':
      value = formatWithDelimiters(cents, 0, '.', ',');
      break;
  }

  return formatString.replace(placeholderRegex, value);
};

2

Answers


  1. Chosen as BEST ANSWER

    I found formatMoney function in my theme and I was able to call it and it worked.

    pipelineVendor.themeCurrency.formatMoney(2000);
    result: $20.00
    

  2. It’s probably too late for the original asker, but for anyone else looking for an answer, I believe the correct call for this function is:

    Shopify.formatMoney(2000, '${{amount}}')
    

    instead of

    Shopify.formatMoney(2000, '$')
    

    based on the comment of the gist author on Github:

    It doesn’t actually care what the currency is. The first argument is
    the unformatted amount. The second argument is the format, which could
    be whatever you need for the currently displayed currency i.e. “£{{
    amount }}”. Then pass it a new amount and new format when you change
    the currency.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search