Skip to content

Commit 4d456e8

Browse files
committed
Add money filters
1 parent cdcd35f commit 4d456e8

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

shopify-dev-utils/convertToGlobalDataStructure.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ module.exports.convertToGlobalDataStructure = function convertToGlobalDataStruct
1717
description: product.node.description,
1818
handle: product.node.handle,
1919
available: product.node.availableForSale,
20-
price: product.node.priceRange,
20+
price: product.node.priceRange.maxVariantPrice, // preserve the entire obj for money-* filters
21+
price_max: product.node.priceRange.maxVariantPrice,
22+
price_min: product.node.priceRange.minVariantPrice,
23+
price_varies:
24+
+product.node.priceRange.maxVariantPrice.amount !==
25+
+product.node.priceRange.minVariantPrice,
2126
url: `/products/${product.node.handle}`,
2227
featured_image:
2328
product.node.images.edges.length > 0
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
module.exports.moneyWithCurrency = function moneyWithCurrency(price) {
2+
if (!price.currencyCode || !price.amount) {
3+
return '';
4+
}
5+
6+
// the price that this object gets has 2 fields it is not the same value in "real" env,
7+
// at real it should be only a number multiplied by 100
8+
return new Intl.NumberFormat('en', {
9+
style: 'currency',
10+
currency: price.currencyCode,
11+
maximumFractionDigits: 2,
12+
}).format(price.amount);
13+
};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
const { moneyWithCurrency } = require('./money_with_currency');
2+
3+
module.exports.moneyWithoutTrailingZeros = function moneyWithoutTrailingZeros(price) {
4+
// the price that this object gets has 2 fields it is not the same value in "real" env,
5+
// at real it should be only a number multiplied by 100
6+
const moneyWithCurrencyAndTrailingZeros = moneyWithCurrency(price);
7+
return moneyWithCurrencyAndTrailingZeros.replace(
8+
/([,.][^0]*(0+))\D*$/,
9+
(match, group, zeros) => {
10+
const cutSize = zeros.length > 1 ? zeros.length + 1 : zeros.length;
11+
return match.replace(group, group.substring(0, group.length - cutSize));
12+
}
13+
);
14+
};

shopify-dev-utils/liquidDev.loader.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const loaderUtils = require('loader-utils');
22
const path = require('path');
33
const { Liquid } = require('liquidjs');
44
const glob = require('glob');
5+
const { moneyWithoutTrailingZeros } = require('./filters/money_without_trailing_zeros');
6+
const { moneyWithCurrency } = require('./filters/money_with_currency');
57
const { within } = require('./filters/within');
68
const { defaultPagination } = require('./filters/default_pagination');
79
const { scriptTag } = require('./filters/script_tag');
@@ -40,6 +42,8 @@ function initEngine() {
4042
engine.registerFilter('script_tag', scriptTag);
4143
engine.registerFilter('default_pagination', defaultPagination);
4244
engine.registerFilter('within', within);
45+
engine.registerFilter('money_with_currency', moneyWithCurrency);
46+
engine.registerFilter('money_without_trailing_zeros', moneyWithoutTrailingZeros);
4347

4448
engine.registerTag('paginate', Paginate);
4549
engine.plugin(liquidSectionTags());

0 commit comments

Comments
 (0)