4
0
forked from crowetic/commerce

usePrice hook

This commit is contained in:
Luis Alvarez
2020-10-12 22:15:53 -05:00
parent a37c02ff57
commit 1629f718b0
6 changed files with 81 additions and 68 deletions

View File

@@ -1,26 +0,0 @@
import formatPrice from './format-price'
export default function formatVariantPrice({
listPrice,
salePrice,
currencyCode,
locale,
}: {
listPrice: number
salePrice: number
currencyCode: string
locale: string
}) {
const hasDiscount = listPrice > salePrice
const formatDiscount = new Intl.NumberFormat(locale, { style: 'percent' })
const discount = hasDiscount
? formatDiscount.format((listPrice - salePrice) / listPrice)
: null
const price = formatPrice({ amount: salePrice, currencyCode, locale })
const compareAtPrice = hasDiscount
? formatPrice({ amount: listPrice, currencyCode, locale })
: null
return { price, compareAtPrice, discount }
}

View File

@@ -1,16 +0,0 @@
export default function formatPrice({
amount,
currencyCode,
locale,
}: {
amount: number
currencyCode: string
locale: string
}) {
const formatCurrency = new Intl.NumberFormat(locale, {
style: 'currency',
currency: currencyCode,
})
return formatCurrency.format(amount)
}