Skip to content

Instantly share code, notes, and snippets.

@stewartknapman
Created February 27, 2017 23:25
Show Gist options
  • Star 43 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
  • Save stewartknapman/8d8733ea58d2314c373e94114472d44c to your computer and use it in GitHub Desktop.
Save stewartknapman/8d8733ea58d2314c373e94114472d44c to your computer and use it in GitHub Desktop.
The Shopify.formatMoney method extracted from option_selection.js for stand-alone purposes.
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)(?=(\d\d\d)+(?!\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);
};
@shriDeveloper
Copy link

Hi , I am using

`
var Shopify = Shopify || {};
Shopify.money_format = "{{ shop.money_format }}";
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)(?=(\d\d\d)+(?!\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);

};`

I am getting an error
Uncaught (in promise) TypeError: Cannot read properties of null (reading '1')

The error is only raised at the first time i.e when someone clicks on a particular product , but error disappears when the same page is refreshed.

Would love some help here.

@SimonMaze
Copy link

@shriDeveloper Make sure the method is not inside a .liquid file or use {% raw %}{{ amount }}{% endraw %} in order to escape the Liquid parsing of {{ amount }} (which parses to an empty string)

@james0r
Copy link

james0r commented Aug 10, 2023

As AlpineJS Magic property if anyone wants it -> https://gist.github.com/james0r/e5a02db44beba8b5fc447e78615c1eb7

@cameronbartlett
Copy link

This was exactly what I was looking for and works like a charm, you sir are an absolute legend.

@ayoubkhan558
Copy link

there was a slight error in the code

function formatMoney(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)(?=(\d\d\d)+(?!\d))/g, '$1' + thousands),
      cents = parts[1] ? (decimal + parts[1]) : '';

    return dollars + cents;
  }

  var match = formatString.match(placeholderRegex);

  if (match) {
    switch (match[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);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment