Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fixed locale formatting for Bulk Price editor

Damian Legawiecb432d941b59e
diff --git a/admin/app/javascript/spree/admin/controllers/bulk_editor_controller.js b/admin/app/javascript/spree/admin/controllers/bulk_editor_controller.js
index 98fa6ea31c..572754d983 100644
--- a/admin/app/javascript/spree/admin/controllers/bulk_editor_controller.js
+++ b/admin/app/javascript/spree/admin/controllers/bulk_editor_controller.js
@@ -49,6 +49,12 @@ import { Controller } from '@hotwired/stimulus'
export default class extends Controller {
static targets = ['table', 'row', 'cell', 'saveButton', 'statusBar']
+ static values = {
+ locale: { type: String, default: 'en' },
+ decimalSeparator: { type: String, default: '.' },
+ thousandsSeparator: { type: String, default: ',' }
+ }
+
connect() {
this.dirtyInputs = new Set()
this.selectedCells = new Set()
@@ -73,16 +79,21 @@ export default class extends Controller {
this.boundHandleBeforeUnload = this.handleBeforeUnload.bind(this)
this.boundHandleMouseUp = this.handleMouseUp.bind(this)
this.boundHandleMouseMove = this.handleMouseMove.bind(this)
+ this.boundNormalizeBeforeSubmit = this.normalizeBeforeSubmit.bind(this)
window.addEventListener('beforeunload', this.boundHandleBeforeUnload)
document.addEventListener('mouseup', this.boundHandleMouseUp)
document.addEventListener('mousemove', this.boundHandleMouseMove)
+
+ // Listen for form submission to normalize values
+ this.element.addEventListener('submit', this.boundNormalizeBeforeSubmit)
}
disconnect() {
window.removeEventListener('beforeunload', this.boundHandleBeforeUnload)
document.removeEventListener('mouseup', this.boundHandleMouseUp)
document.removeEventListener('mousemove', this.boundHandleMouseMove)
+ this.element.removeEventListener('submit', this.boundNormalizeBeforeSubmit)
}
// ==================== Fill Handle ====================
@@ -771,4 +782,86 @@ export default class extends Controller {
get isDirty() {
return this.dirtyInputs.size > 0
}
+
+ // ==================== Locale-aware Number Handling ====================
+
+ /**
+ * Normalizes a locale-formatted number string to standard decimal format
+ * e.g., "1.234,56" (German) -> "1234.56"
+ * e.g., "1,234.56" (English) -> "1234.56"
+ * @param {string} value - The locale-formatted number string
+ * @returns {string} The normalized number string with "." as decimal separator
+ */
+ normalizeNumber(value) {
+ if (value === null || value === undefined) return ''
+
+ let stringValue = String(value).trim()
+ if (stringValue === '') return ''
+
+ // Detect the decimal separator by finding the last separator character
+ // This handles both "1,234.56" (en) and "1.234,56" (de/pl) formats
+ const lastComma = stringValue.lastIndexOf(',')
+ const lastDot = stringValue.lastIndexOf('.')
+
+ let decimalSeparator = '.'
+ let thousandsSeparator = ','
+
+ // If comma comes after dot, comma is the decimal separator (European format)
+ // Also treat comma as decimal if there's no dot and comma has 1-2 digits after it
+ if (lastComma > lastDot) {
+ decimalSeparator = ','
+ thousandsSeparator = '.'
+ } else if (lastDot === -1 && lastComma !== -1) {
+ // No dot present, check if comma looks like a decimal separator
+ // (has 1-3 digits after it, typical for currency)
+ const afterComma = stringValue.substring(lastComma + 1)
+ if (/^\d{1,3}$/.test(afterComma)) {
+ decimalSeparator = ','
+ thousandsSeparator = '.'
+ }
+ }
+
+ // Remove thousands separators
+ stringValue = stringValue.split(thousandsSeparator).join('')
+
+ // Replace decimal separator with standard "."
+ if (decimalSeparator !== '.') {
+ stringValue = stringValue.replace(decimalSeparator, '.')
+ }
+
+ // Remove any non-numeric characters except "." and "-"
+ stringValue = stringValue.replace(/[^0-9.\-]/g, '')
+
+ return stringValue
+ }
+
+ /**
+ * Formats a number for display using the configured locale
+ * @param {number|string} value - The number to format
+ * @returns {string} The formatted number string
+ */
+ formatNumber(value) {
+ if (value === null || value === undefined || value === '') return ''
+
+ const number = parseFloat(value)
+ if (!Number.isFinite(number)) return ''
+
+ return number.toLocaleString(this.localeValue, {
+ minimumFractionDigits: 2,
+ maximumFractionDigits: 2,
+ useGrouping: false
+ })
+ }
+
+ /**
+ * Normalizes all cell values before form submission
+ * @param {Event} event - The form submit event
+ */
+ normalizeBeforeSubmit(event) {
+ this.cellTargets.forEach((cell) => {
+ if (cell.value) {
+ cell.value = this.normalizeNumber(cell.value)
+ }
+ })
+ }
}
diff --git a/admin/app/views/spree/admin/price_lists/edit_prices.html.erb b/admin/app/views/spree/admin/price_lists/edit_prices.html.erb
index 44cbe49d12..257806d8c2 100644
--- a/admin/app/views/spree/admin/price_lists/edit_prices.html.erb
+++ b/admin/app/views/spree/admin/price_lists/edit_prices.html.erb
@@ -7,12 +7,19 @@
<%= turbo_save_button_tag Spree.t('actions.save'), form: "edit_price_list_#{@price_list.id}" %>
<% end %>
+<%# Use currency's decimal mark for consistent formatting regardless of I18n locale %>
+<% currency_obj = ::Money::Currency.find(@currency) %>
+<% decimal_separator = currency_obj.decimal_mark %>
+<% thousands_separator = currency_obj.thousands_separator %>
<%= form_with model: [:admin, @price_list],
method: :patch,
html: { class: 'relative', id: "edit_price_list_#{@price_list.id}" },
data: {
controller: 'bulk-editor',
action: 'keydown.ctrl+a->bulk-editor#selectAll keydown.meta+a->bulk-editor#selectAll',
+ bulk_editor_locale_value: I18n.locale,
+ bulk_editor_decimal_separator_value: decimal_separator,
+ bulk_editor_thousands_separator_value: thousands_separator,
} do |f| %>
<div class="card mb-6">
@@ -71,32 +78,36 @@
<%= pf.hidden_field :currency %>
<label class="flex items-center cursor-cell m-0">
<span class="pl-4 text-gray-500 pointer-events-none"><%= @currency_symbol %></span>
- <%= pf.number_field :amount,
- step: 0.01,
- placeholder: '0.00',
+ <% formatted_amount = price.amount.present? ? format('%.2f', price.amount).tr('.', decimal_separator) : '' %>
+ <%= pf.text_field :amount,
+ value: formatted_amount,
+ placeholder: "",
+ inputmode: 'decimal',
class: 'w-full pl-1 pr-4 py-2 border-0 shadow-none outline-none text-right tabular-nums bg-transparent',
data: {
bulk_editor_target: 'cell',
row: row_index,
col: 0,
action: bulk_editor_cell_actions,
- original_value: price.amount
+ original_value: formatted_amount
} %>
</label>
</td>
<td class="p-0 w-15 bulk-editor-cell">
<label class="flex items-center cursor-cell m-0">
<span class="pl-4 text-gray-500 pointer-events-none"><%= @currency_symbol %></span>
- <%= pf.number_field :compare_at_amount,
- step: 0.01,
- placeholder: '0.00',
+ <% formatted_compare_at = price.compare_at_amount.present? ? format('%.2f', price.compare_at_amount).tr('.', decimal_separator) : '' %>
+ <%= pf.text_field :compare_at_amount,
+ value: formatted_compare_at,
+ placeholder: "",
+ inputmode: 'decimal',
class: 'w-full pl-1 pr-4 py-2 border-0 shadow-none outline-none text-right tabular-nums bg-transparent',
data: {
bulk_editor_target: 'cell',
row: row_index,
col: 1,
action: bulk_editor_cell_actions,
- original_value: price.compare_at_amount
+ original_value: formatted_compare_at
} %>
</label>
</td>