Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions docs/plugins/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,24 @@ client and server side.


![i18n flow](./i18n-plugin-diagram.png)

## Currency

We expose `$fc` (format currency) function as a plugin to allow you to convert any number or string into a formatted currency string. Under the hood plugins uses Intl.NumberFormat class to handle formatting and therefore exposes the same options configuration.
Here are few examples of how you can use it in templates.
```javascript
$fc(productGetters.getPrice(product).regular) // by default vsf-currency cookie is used to define target currency
$fc(productGetters.getPrice(product).regular, { currency: 'USD' }) // you can easily override this behaviour by passing configuration object
```

Configuration object is an interface Intl.NumberFormatOptions, check it for all available options.

---
***NOTE***

Why we are not using built-in i18n package currency configuration and $n function to cover currency displaying? The answer is because we must be able to handle multiple currencies within one store but in i18n package, currency and locale are in 1:1 relation when in Magento one store can have multiple languages and currencies set. Currency and locale are necessarily disjointed which is not true for i18n plugin.

You can still use all i18n native functions to perform any formatting though.

---

8 changes: 4 additions & 4 deletions packages/theme/components/CartSidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@
:image="cartGetters.getItemImage(product)"
:title="cartGetters.getItemName(product)"
:regular-price="
$n(cartGetters.getItemPrice(product).regular, 'currency')
$fc(cartGetters.getItemPrice(product).regular)
"
:special-price="
cartGetters.productHasSpecialPrice(product)
? getItemPrice(product).special &&
$n(cartGetters.getItemPrice(product).special, 'currency')
$fc(cartGetters.getItemPrice(product).special)
: ''
"
:link="
Expand Down Expand Up @@ -196,11 +196,11 @@
>
<template #value>
<SfPrice
:regular="$n(totals.subtotal, 'currency')"
:regular="$fc(totals.subtotal)"
:special="
totals.subtotal <= totals.special
? ''
: $n(totals.special, 'currency')
: $fc(totals.special)
"
/>
</template>
Expand Down
9 changes: 5 additions & 4 deletions packages/theme/components/Checkout/CartPreview.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,25 @@
/>
<SfProperty
:name="$t('Subtotal')"
:value="$n(totals.subtotal, 'currency')"
:value="$fc(totals.subtotal)"
:class="['sf-property--full-width', 'sf-property--large property']"
/>
<SfProperty
v-if="hasDiscounts"
:name="$t('Discount')"
:value="$n(discountsAmount, 'currency')"
:value="$fc(discountsAmount)"
class="sf-property--full-width sf-property--small property"
/>
<SfProperty
v-if="selectedShippingMethod"
:name="$t('Shipping')"
:value="$n(getShippingMethodPrice(selectedShippingMethod), 'currency')"
:value="$fc(getShippingMethodPrice(selectedShippingMethod))"
class="sf-property--full-width sf-property--large property"
/>

<SfProperty
:name="$t('Total')"
:value="$n(totals.total, 'currency')"
:value="$fc(totals.total)"
class="sf-property--full-width sf-property--large property-total"
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
<div class="sf-radio__label shipping__label">
<div>{{ method.carrier_title }}</div>
<div v-if="method && (method.amount || method.price_incl_tax)">
{{ $n(getShippingMethodPrice(method), 'currency') }}
{{ $fc(getShippingMethodPrice(method)) }}
</div>
</div>
</template>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
>
{{ productGetters.getName(option.product) }}
<SfPrice
:regular="$n(productGetters.getPrice(option.product).regular, 'currency')"
:special="productGetters.getPrice(option.product).special && $n(productGetters.getPrice(option.product).special, 'currency')"
:regular="$fc(productGetters.getPrice(option.product).regular)"
:special="productGetters.getPrice(option.product).special && $fc(productGetters.getPrice(option.product).special)"
/>
</template>
<template
Expand All @@ -43,8 +43,8 @@
>
<template #description>
<SfPrice
:regular="$n(productGetters.getPrice(option.product).regular, 'currency')"
:special="productGetters.getPrice(option.product).special && $n(productGetters.getPrice(option.product).special, 'currency')"
:regular="$fc(productGetters.getPrice(option.product).regular)"
:special="productGetters.getPrice(option.product).special && $fc(productGetters.getPrice(option.product).special)"
/>
</template>
</SfRadio>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
<div>
<p>{{ productGetters.getName(groupedItem.product) }}</p>
<SfPrice
:regular="$n(productGetters.getPrice(groupedItem.product).regular, 'currency')"
:special="productGetters.getPrice(groupedItem.product).special && $n(productGetters.getPrice(groupedItem.product).special, 'currency')"
:regular="$fc(productGetters.getPrice(groupedItem.product).regular)"
:special="productGetters.getPrice(groupedItem.product).special && $fc(productGetters.getPrice(groupedItem.product).special)"
/>
</div>
<SfQuantitySelector
Expand Down
4 changes: 2 additions & 2 deletions packages/theme/components/ProductsCarousel.vue
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
<SfProductCard
:title="productGetters.getName(product)"
:image="productGetters.getProductThumbnailImage(product)"
:regular-price="$n(productGetters.getPrice(product).regular, 'currency')"
:special-price="productGetters.getPrice(product).special && $n(productGetters.getPrice(product).special, 'currency')"
:regular-price="$fc(productGetters.getPrice(product).regular)"
:special-price="productGetters.getPrice(product).special && $fc(productGetters.getPrice(product).special)"
:link="localePath(`/p/${productGetters.getProductSku(product)}${productGetters.getSlug(product, product.categories[0])}`)"
:max-rating="5"
:score-rating="productGetters.getAverageRating(product)"
Expand Down
4 changes: 2 additions & 2 deletions packages/theme/components/SearchResults.vue
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
v-for="(product, index) in products"
:key="index"
class="result-card"
:regular-price="$n(productGetters.getPrice(product).regular, 'currency')"
:regular-price="$fc(productGetters.getPrice(product).regular)"
:max-rating="5"
:score-rating="productGetters.getAverageRating(product)"
:reviews-count="productGetters.getTotalReviews(product)"
Expand All @@ -89,7 +89,7 @@
v-for="(product, index) in products"
:key="index"
class="result-card"
:regular-price="$n(productGetters.getPrice(product).regular, 'currency')"
:regular-price="$fc(productGetters.getPrice(product).regular)"
:max-rating="5"
:score-rating="productGetters.getAverageRating(product)"
:reviews-count="productGetters.getTotalReviews(product)"
Expand Down
6 changes: 3 additions & 3 deletions packages/theme/components/WishlistSidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@
:key="i"
:image="wishlistGetters.getItemImage(product)"
:title="wishlistGetters.getItemName(product)"
:regular-price="$n(wishlistGetters.getItemPrice(product).regular, 'currency')"
:regular-price="$fc(wishlistGetters.getItemPrice(product).regular)"
:link="localePath(`/p/${wishlistGetters.getItemSku(product)}${productGetters.getSlug(product.product, product.product.categories[0])}`)"
:special-price="wishlistGetters.getItemPrice(product).special && $n(wishlistGetters.getItemPrice(product).special, 'currency')"
:special-price="wishlistGetters.getItemPrice(product).special && $fc(wishlistGetters.getItemPrice(product).special)"
:stock="99999"
class="collected-product"
@click:remove="removeItem({ product: product.product })"
Expand Down Expand Up @@ -107,7 +107,7 @@
<span class="my-wishlist__total-price-label">Total price:</span>
</template>
<template #value>
<SfPrice :regular="$n(totals.subtotal, 'currency')" />
<SfPrice :regular="$fc(totals.subtotal)" />
</template>
</SfProperty>
</div>
Expand Down
16 changes: 16 additions & 0 deletions packages/theme/helpers/__tests__/formatCurrency.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import formatCurrency from '~/helpers/formatCurrency';

describe('formatCurrency()', () => {
it('executed without required options should throw error', () => {
expect(() => { formatCurrency(100, 'en-US', {}); }).toThrowError();
});

it('should return formatted currency string', () => {
const currency = 'EUR';
jest.spyOn(Intl, 'NumberFormat').mockImplementation(() => ({
format: (value) => `${value}${currency}`,
}));
const result = formatCurrency(123, 'de-DE', { currency });
expect(result).toBe('123EUR');
});
});
11 changes: 11 additions & 0 deletions packages/theme/helpers/formatCurrency.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const formatCurrency = (value: number | string, locale: string, options: Intl.NumberFormatOptions): string => {
if (typeof value === 'string') {
// eslint-disable-next-line no-param-reassign
value = Number(value);
}
// eslint-disable-next-line no-param-reassign

return new Intl.NumberFormat(locale, { style: 'currency', ...options }).format(value);
};

export default formatCurrency;
1 change: 1 addition & 0 deletions packages/theme/nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ export default {
plugins: [
'~/plugins/token-expired',
'~/plugins/i18n',
'~/plugins/fcPlugin',
],
router: {
extendRoutes(routes) {
Expand Down
8 changes: 4 additions & 4 deletions packages/theme/pages/Category.vue
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@
:style="{ '--index': i }"
:title="productGetters.getName(product)"
:image="productGetters.getProductThumbnailImage(product)"
:regular-price="$n(productGetters.getPrice(product).regular, 'currency')"
:special-price="productGetters.getPrice(product).special && $n(productGetters.getPrice(product).special, 'currency')"
:regular-price="$fc(productGetters.getPrice(product).regular)"
:special-price="productGetters.getPrice(product).special && $fc(productGetters.getPrice(product).special)"
:score-rating="productGetters.getAverageRating(product)"
:reviews-count="productGetters.getTotalReviews(product)"
:show-add-to-cart-button="true"
Expand Down Expand Up @@ -158,8 +158,8 @@
:title="productGetters.getName(product)"
:description="productGetters.getDescription(product)"
:image="productGetters.getProductThumbnailImage(product)"
:regular-price="$n(productGetters.getPrice(product).regular, 'currency')"
:special-price="productGetters.getPrice(product).special && $n(productGetters.getPrice(product).special, 'currency')"
:regular-price="$fc(productGetters.getPrice(product).regular)"
:special-price="productGetters.getPrice(product).special && $fc(productGetters.getPrice(product).special)"
:score-rating="productGetters.getAverageRating(product)"
:reviews-count="productGetters.getTotalReviews(product)"
:is-in-wishlist="isInWishlist({product})"
Expand Down
12 changes: 6 additions & 6 deletions packages/theme/pages/Checkout/Payment.vue
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@
</SfTableData>
<SfTableData class="table__data price">
<SfPrice
:regular="$n(cartGetters.getItemPrice(product).regular, 'currency')"
:special="cartGetters.getItemPrice(product).special && $n(cartGetters.getItemPrice(product).special, 'currency')"
:regular="$fc(cartGetters.getItemPrice(product).regular)"
:special="cartGetters.getItemPrice(product).special && $fc(cartGetters.getItemPrice(product).special)"
class="product-price"
/>
</SfTableData>
Expand All @@ -77,13 +77,13 @@
<div class="summary__total">
<SfProperty
name="Subtotal"
:value="$n(totals.subtotal, 'currency')"
:value="$fc(totals.subtotal)"
class="sf-property--full-width property"
/>
<SfProperty
v-if="hasDiscounts"
:name="$t('Discount')"
:value="$n(discountsAmount, 'currency')"
:value="$fc(discountsAmount)"
class="sf-property--full-width sf-property--small property"
/>
</div>
Expand All @@ -92,7 +92,7 @@
class="summary__total"
>
<SfProperty
:value="$n(getShippingMethodPrice(selectedShippingMethod), 'currency')"
:value="$fc(getShippingMethodPrice(selectedShippingMethod))"
class="sf-property--full-width property"
>
<template #name>
Expand All @@ -107,7 +107,7 @@

<SfProperty
name="Total price"
:value="$n(totals.total, 'currency')"
:value="$fc(totals.total)"
class="sf-property--full-width sf-property--large summary__property-total"
/>

Expand Down
8 changes: 4 additions & 4 deletions packages/theme/pages/MyAccount/MyWishlist.vue
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@
)}${productGetters.getSlug(product.product, product.product.categories[0])}`
)
"
:regular-price="$n(productGetters.getPrice(product.product).regular, 'currency')"
:regular-price="$fc(productGetters.getPrice(product.product).regular)"
:reviews-count="productGetters.getTotalReviews(product.product)"
:score-rating="productGetters.getAverageRating(product.product)"
:show-add-to-cart-button="true"
:special-price="productGetters.getPrice(product.product).special
&& $n(productGetters.getPrice(product.product).special, 'currency')"
&& $fc(productGetters.getPrice(product.product).special)"
:style="{ '--index': i }"
:title="productGetters.getName(product.product)"
wishlist-icon
Expand Down Expand Up @@ -110,11 +110,11 @@
)}${productGetters.getSlug(product.product, product.product.categories[0])}`
)
"
:regular-price="$n(productGetters.getPrice(product.product).regular, 'currency')"
:regular-price="$fc(productGetters.getPrice(product.product).regular)"
:reviews-count="productGetters.getTotalReviews(product.product)"
:score-rating="productGetters.getAverageRating(product.product)"
:special-price="productGetters.getPrice(product.product).special
&& $n(productGetters.getPrice(product.product).special, 'currency')"
&& $fc(productGetters.getPrice(product.product).special)"
:style="{ '--index': i }"
:title="productGetters.getName(product.product)"
wishlist-icon
Expand Down
6 changes: 3 additions & 3 deletions packages/theme/pages/MyAccount/OrderHistory.vue
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
/>
<SfProperty
name="Total"
:value="$n(orderGetters.getPrice(currentOrder), 'currency')"
:value="$fc(orderGetters.getPrice(currentOrder))"
class="sf-property--full-width property"
/>
</div>
Expand All @@ -55,7 +55,7 @@
</nuxt-link>
</SfTableData>
<SfTableData>{{ orderGetters.getItemQty(item) }}</SfTableData>
<SfTableData>{{ $n(orderGetters.getItemPrice(item), 'currency') }}</SfTableData>
<SfTableData>{{ $fc(orderGetters.getItemPrice(item)) }}</SfTableData>
</SfTableRow>
</SfTable>
</div>
Expand Down Expand Up @@ -97,7 +97,7 @@
>
<SfTableData>{{ orderGetters.getId(order) }}</SfTableData>
<SfTableData>{{ orderGetters.getDate(order) }}</SfTableData>
<SfTableData>{{ $n(orderGetters.getPrice(order), 'currency') }}</SfTableData>
<SfTableData>{{ $fc(orderGetters.getPrice(order)) }}</SfTableData>
<SfTableData>
<span :class="getStatusTextClass(order)">{{ orderGetters.getStatus(order) }}</span>
</SfTableData>
Expand Down
4 changes: 2 additions & 2 deletions packages/theme/pages/Product.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@
</div>
<div class="product__price-and-rating">
<SfPrice
:regular="$n(productPrice, 'currency')"
:special="productSpecialPrice && $n(productSpecialPrice, 'currency')"
:regular="$fc(productPrice)"
:special="productSpecialPrice && $fc(productSpecialPrice)"
/>
<div>
<div class="product__rating">
Expand Down
10 changes: 10 additions & 0 deletions packages/theme/plugins/__tests__/fcPlugin.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import fcPlugin from '~/plugins/fcPlugin';

const injectFnMock = jest.fn();

describe('Format currency plugin', () => {
it('should inject $fc', () => {
fcPlugin({ app: {} }, injectFnMock);
expect(injectFnMock).toHaveBeenCalledTimes(1);
});
});
21 changes: 21 additions & 0 deletions packages/theme/plugins/fcPlugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import cookieNames from '~/enums/cookieNameEnum';
import formatCurrency from '~/helpers/formatCurrency';

declare module 'vue/types/vue' {
interface Vue {
$fc(value: number | string): string;
$fc(value: number | string, options?: Intl.NumberFormatOptions): string;
$fc(value: number | string, locale?: string, options?: Intl.NumberFormatOptions): string;
}
}

export default ({ app }, inject) => {
inject('fc', (value: number | string, locale?: string, options = {}): string => {
// eslint-disable-next-line no-param-reassign
locale = locale || app.i18n?.localeProperties?.iso.replace('_', '-');
// eslint-disable-next-line no-param-reassign
options = { currency: app.$cookies.get(cookieNames.currencyCookieName), ...options };

return formatCurrency(value, locale, options);
});
};
2 changes: 2 additions & 0 deletions packages/theme/test-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import { render } from '@testing-library/vue';

const $t = (text) => text;
const $n = (text) => text;
const $fc = (text) => text;
const localePath = (path) => path;
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
const customRender = (component, options = {}, callback = null) => render(component, {
mocks: {
$t,
$n,
$fc,
localePath,
$nuxt: {
context: {
Expand Down