@scayle/storefront-nuxt
Version:
Nuxt integration for the SCAYLE Commerce Engine and Storefront API
37 lines (36 loc) • 996 B
JavaScript
import { computed, toValue } from "vue";
export function useProductPrice(price) {
const appliedReductions = computed(() => {
const reductions = toValue(price).appliedReductions ?? [];
return reductions.toReversed();
});
const strikeThroughPrices = computed(() => {
return appliedReductions.value.reduce(
({ prices, currentPrice }, { amount }) => {
currentPrice += amount.absoluteWithTax;
return {
currentPrice,
prices: [...prices, currentPrice]
};
},
{
prices: [],
currentPrice: toValue(price).withTax
}
).prices;
});
const relativeReductions = computed(
() => appliedReductions.value.map(({ amount, category }) => {
return { value: Math.round(amount.relative * 100), category };
})
);
const totalPrice = computed(() => {
return toValue(price).withTax;
});
return {
appliedReductions,
strikeThroughPrices,
relativeReductions,
totalPrice
};
}