Commit 6731036
feat(plugin-ecommerce): add locale-aware currency formatting and symbol positioning (#15139)
This PR introduces automatic, locale-aware currency formatting using the
native Intl.NumberFormat API.
It also standardizes currency symbol positions and separators across the
frontend, ensuring consistent price display for different currencies in
Payload e-commerce.
**Changes**
1. useCurrency hook
- Added locale support in formatCurrency.
- Replaced manual string formatting with Intl.NumberFormat:
```
return new Intl.NumberFormat(locale, {
style: 'currency',
currency: code,
minimumFractionDigits: decimals,
maximumFractionDigits: decimals,
}).format(value / Math.pow(10, decimals))
```
Automatically handles:
- Decimal separators (. vs ,) depending on locale
- Currency placement (before or after the value)
- Correct number of fraction digits
- Optional locale parameter allows overriding per call (default: 'en')
**2. Currency display settings**
Standardized symbol placement and separator:
```
<div className="priceCell">
{currency.symbolPosition === 'before' ? `<span className="currencySymbol">${currency.symbol}</span>` : ''}
{currency.symbolPosition === 'before' && currency.symbolSeparator}
<span className="priceValue">{convertFromBaseValue({ baseValue: cellData, currency })}</span>
{currency.symbolPosition === 'after' && currency.symbolSeparator}
{currency.symbolPosition === 'after' ? `<span className="currencySymbol">${currency.symbol}</span>` : ''}
</div>
```
Ensures correct rendering for currencies that place the symbol before
(EUR, USD, GBP) or after (PLN).
**3. Predefined currencies**
Updated standard currency definitions:
```
export const EUR: Currency = { code: 'EUR', decimals: 2, label: 'Euro', symbol: '€', symbolPosition: 'before', symbolSeparator: '' }
export const USD: Currency = { code: 'USD', decimals: 2, label: 'US Dollar', symbol: '$', symbolPosition: 'before', symbolSeparator: '' }
export const GBP: Currency = { code: 'GBP', decimals: 2, label: 'British Pound', symbol: '£', symbolPosition: 'before', symbolSeparator: '' }
```
**4. Example: currenciesConfig in Payload**
```
import { EUR as BaseEUR, USD } from '@payloadcms/plugin-ecommerce';
import type { Currency } from '@payloadcms/plugin-ecommerce/types';
const PLN = {
code: 'PLN',
decimals: 2,
label: 'Polski złoty',
symbol: 'zł',
symbolPosition: 'after',
symbolSeparator: ' ',
} satisfies Currency;
export const currenciesConfig = {
supportedCurrencies: [
PLN,
USD,
BaseEUR,
],
defaultCurrency: 'PLN',
};
```
<img width="576" height="641" alt="product-example"
src="https://github.com/user-attachments/assets/4965ac38-a8cc-44ab-b8d8-0f3d9082399c"
/>
---------
Co-authored-by: Paul Popus <paul@payloadcms.com>1 parent 48db8c1 commit 6731036
6 files changed
Lines changed: 216 additions & 48 deletions
File tree
- packages/plugin-ecommerce/src
- react/provider
- types
- ui
- PriceCell
- PriceRowLabel
- test/plugin-ecommerce
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1116 | 1116 | | |
1117 | 1117 | | |
1118 | 1118 | | |
1119 | | - | |
| 1119 | + | |
1120 | 1120 | | |
1121 | 1121 | | |
1122 | 1122 | | |
1123 | 1123 | | |
1124 | 1124 | | |
1125 | | - | |
1126 | 1125 | | |
1127 | 1126 | | |
1128 | 1127 | | |
1129 | 1128 | | |
1130 | | - | |
1131 | | - | |
1132 | | - | |
| 1129 | + | |
1133 | 1130 | | |
1134 | | - | |
1135 | | - | |
| 1131 | + | |
1136 | 1132 | | |
1137 | | - | |
1138 | | - | |
| 1133 | + | |
| 1134 | + | |
| 1135 | + | |
| 1136 | + | |
| 1137 | + | |
| 1138 | + | |
| 1139 | + | |
1139 | 1140 | | |
1140 | 1141 | | |
1141 | 1142 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
233 | 233 | | |
234 | 234 | | |
235 | 235 | | |
236 | | - | |
| 236 | + | |
237 | 237 | | |
238 | 238 | | |
239 | 239 | | |
| |||
252 | 252 | | |
253 | 253 | | |
254 | 254 | | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
255 | 260 | | |
256 | 261 | | |
257 | 262 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
| 2 | + | |
2 | 3 | | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
8 | | - | |
| 9 | + | |
9 | 10 | | |
10 | 11 | | |
11 | 12 | | |
| |||
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
19 | | - | |
| 20 | + | |
20 | 21 | | |
21 | 22 | | |
22 | 23 | | |
| |||
40 | 41 | | |
41 | 42 | | |
42 | 43 | | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
| 44 | + | |
49 | 45 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | 2 | | |
3 | | - | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
| |||
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
| 19 | + | |
19 | 20 | | |
20 | 21 | | |
21 | 22 | | |
| |||
32 | 33 | | |
33 | 34 | | |
34 | 35 | | |
35 | | - | |
36 | | - | |
37 | | - | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
38 | 39 | | |
39 | 40 | | |
40 | | - | |
41 | | - | |
| 41 | + | |
| 42 | + | |
42 | 43 | | |
43 | 44 | | |
44 | 45 | | |
45 | 46 | | |
46 | 47 | | |
47 | 48 | | |
48 | | - | |
49 | | - | |
50 | | - | |
51 | | - | |
| 49 | + | |
52 | 50 | | |
53 | 51 | | |
54 | 52 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
44 | 44 | | |
45 | 45 | | |
46 | 46 | | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
0 commit comments