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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Fix price parsing for non-English locales (#13627)
Damian Legawiecbdc10796a9b2
diff --git a/backend/engines/admin/app/javascript/spree/admin/controllers/bulk_editor_controller.js b/backend/engines/admin/app/javascript/spree/admin/controllers/bulk_editor_controller.jsindex 572754d983..f124f87823 100644--- a/backend/engines/admin/app/javascript/spree/admin/controllers/bulk_editor_controller.js+++ b/backend/engines/admin/app/javascript/spree/admin/controllers/bulk_editor_controller.js@@ -79,21 +79,16 @@ 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 ====================@@ -783,85 +778,4 @@ export default class extends Controller {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/backend/engines/admin/app/javascript/spree/admin/controllers/money_field_controller.js b/backend/engines/admin/app/javascript/spree/admin/controllers/money_field_controller.jsindex 92b5e4f6d7..bfeed4ea64 100644--- a/backend/engines/admin/app/javascript/spree/admin/controllers/money_field_controller.js+++ b/backend/engines/admin/app/javascript/spree/admin/controllers/money_field_controller.js@@ -4,11 +4,10 @@ import { Controller } from '@hotwired/stimulus'* MoneyFieldController** A Stimulus controller for locale-aware money/price input fields.- * Handles formatting for display and normalizing values before form submission.+ * Handles formatting values for display in the user's locale format.** Features:* - Displays amounts in the user's locale format (e.g., "1.234,56" for German, "1,234.56" for English)- * - Normalizes values to standard decimal format (with "." as decimal separator) before form submission* - Supports optional currency symbol display** Usage:@@ -32,23 +31,10 @@ export default class extends Controller {}connect() {- this.form = this.element.closest('form')-- if (this.form) {- this.boundNormalizeBeforeSubmit = this.normalizeBeforeSubmit.bind(this)- this.form.addEventListener('submit', this.boundNormalizeBeforeSubmit)- }-// Format the initial value for displaythis.formatForDisplay()}- disconnect() {- if (this.form && this.boundNormalizeBeforeSubmit) {- this.form.removeEventListener('submit', this.boundNormalizeBeforeSubmit)- }- }-/*** Called on blur to format the value for display*/@@ -139,12 +125,4 @@ export default class extends Controller {return stringValue}- /**- * Normalizes the value before form submission- * @param {Event} event - The form submit event- */- normalizeBeforeSubmit(event) {- const normalizedValue = this.normalizeValue(this.element.value)- this.element.value = normalizedValue- }}diff --git a/backend/engines/admin/spec/features/spree/admin/products/products_spec.rb b/backend/engines/admin/spec/features/spree/admin/products/products_spec.rbindex 8a6438bde2..fffd5eccba 100644--- a/backend/engines/admin/spec/features/spree/admin/products/products_spec.rb+++ b/backend/engines/admin/spec/features/spree/admin/products/products_spec.rb@@ -208,6 +208,23 @@ describe 'Products', type: :feature doexpect(product.master.stock_items.first.count_on_hand).to eq(10)end+ context 'with non-English locale using comma as decimal separator', js: true do+ before do+ I18n.backend.store_translations(:de, number: { currency: { format: { separator: ',', delimiter: '.' } } })+ allow(store).to receive(:default_locale).and_return('de')+ end++ it 'correctly saves price without multiplying by 100' do+ visit spree.edit_admin_product_path(product)++ fill_in 'product_master_attributes_prices_attributes_0_amount', with: '123,00'+ within('#page-header') { click_button 'Update' }++ expect(page).to have_content('successfully updated')+ expect(product.reload.price_in('USD').amount).to eq(123.0)+ end+ end+it 'allows adding new options', js: true do# Add the first option - Colorfind('span', text: Spree.t('admin.variants_form.add_option.empty')).clickdiff --git a/backend/engines/core/spec/lib/spree/localized_number_spec.rb b/backend/engines/core/spec/lib/spree/localized_number_spec.rbindex 8db888d2c9..ed2b1706f6 100644--- a/backend/engines/core/spec/lib/spree/localized_number_spec.rb+++ b/backend/engines/core/spec/lib/spree/localized_number_spec.rb@@ -51,5 +51,16 @@ describe Spree::LocalizedNumber doexpect(subject.class.parse('')).to be 0endend++ context 'when input uses the locale decimal separator (as sent by money_field_controller.js)' do+ it 'correctly parses comma-decimal values with German locale' do+ allow(I18n).to receive(:locale).and_return(:de)+ allow(I18n.config).to receive(:locale).and_return(:de)++ # money_field_controller.js strips thousands separators but keeps the locale's decimal separator+ expect(subject.class.parse('123,00')).to eq(123.0)+ expect(subject.class.parse('1599,99')).to eq(1599.99)+ end+ endendend