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: auto-switch cart market when currency changes also validate cu… (#13871)

Damian Legawiecb40d87291028
diff --git a/spree/api/spec/controllers/spree/api/v3/store/carts_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/store/carts_controller_spec.rb
index 73830c0842..0badeb47c9 100644
--- a/spree/api/spec/controllers/spree/api/v3/store/carts_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/store/carts_controller_spec.rb
@@ -533,6 +533,28 @@ RSpec.describe Spree::Api::V3::Store::CartsController, type: :controller do
expect(response).to have_http_status(:not_found)
end
+
+ it 'auto-switches market when currency changes' do
+ order.update!(market: market)
+
+ de_country = create(:country, iso: 'DE', name: 'Germany')
+ eu_market = create(:market, :eu, store: store, countries: [de_country])
+
+ patch :update, params: { id: order.prefixed_id, currency: 'EUR' }
+
+ expect(response).to have_http_status(:ok)
+ expect(json_response['currency']).to eq('EUR')
+ expect(json_response['market_id']).to eq(eu_market.prefixed_id)
+ end
+
+ it 'returns validation error when no market exists for currency' do
+ order.update!(market: market)
+
+ patch :update, params: { id: order.prefixed_id, currency: 'GBP' }
+
+ expect(response).to have_http_status(:unprocessable_content)
+ expect(json_response['error']['code']).to eq('validation_error')
+ end
end
it 'auto-advances to payment after address submission' do
diff --git a/spree/core/app/models/concerns/spree/stores/markets.rb b/spree/core/app/models/concerns/spree/stores/markets.rb
index dd83ccf708..bd4ac1a6cf 100644
--- a/spree/core/app/models/concerns/spree/stores/markets.rb
+++ b/spree/core/app/models/concerns/spree/stores/markets.rb
@@ -104,12 +104,14 @@ module Spree
end
end
- private
-
+ # Returns true if the store has markets, false otherwise
+ # @return [Boolean]
def has_markets?
@has_markets ||= persisted? && (markets.loaded? ? markets.any? : markets.exists?)
end
+ private
+
def legacy_supported_currencies_list
([default_currency] + read_attribute(:supported_currencies).to_s.split(',')).uniq.map(&:to_s).map do |code|
::Money::Currency.find(code.strip)
diff --git a/spree/core/app/models/spree/order.rb b/spree/core/app/models/spree/order.rb
index 790fb48d9f..77d059bdd2 100644
--- a/spree/core/app/models/spree/order.rb
+++ b/spree/core/app/models/spree/order.rb
@@ -182,6 +182,8 @@ module Spree
alias_attribute :fulfillment_status, :shipment_state
alias_attribute :payment_status, :payment_state
+ delegate :has_markets?, to: :store, prefix: true
+
accepts_nested_attributes_for :line_items
accepts_nested_attributes_for :bill_address
accepts_nested_attributes_for :ship_address
@@ -195,10 +197,11 @@ module Spree
before_validation :ensure_market_presence
before_validation :ensure_currency_presence
before_validation :ensure_locale_presence
+ before_validation :resolve_market_from_currency, if: -> { persisted? && currency_changed? && !skip_market_resolution }
before_validation :clone_billing_address, if: :use_billing?
before_validation :clone_shipping_address, if: :use_shipping?
- attr_accessor :use_billing, :use_shipping
+ attr_accessor :use_billing, :use_shipping, :skip_market_resolution
before_create :link_by_email
before_update :ensure_updated_shipments, :homogenize_line_item_currencies, if: :currency_changed?
@@ -222,6 +225,7 @@ module Spree
validates :shipment_total, MONEY_VALIDATION
validates :promo_total, NEGATIVE_MONEY_VALIDATION
validates :total, MONEY_VALIDATION
+ validates :market, presence: true, if: :store_has_markets?
validate :currency_must_be_supported_by_store
validate :locale_must_be_supported_by_store
@@ -453,7 +457,7 @@ module Spree
end
def ensure_market_presence
- self.market ||= Spree::Current.market
+ self.market ||= Spree::Current.market || store&.default_market
end
def allow_cancel?
@@ -1056,6 +1060,16 @@ module Spree
end
end
+ # When currency changes, auto-resolve the matching market.
+ # Only applies when the store has markets configured.
+ def resolve_market_from_currency
+ return unless store_has_markets?
+ return if market&.currency == currency
+
+ resolved = store.markets.find_by(currency: currency)
+ self.market = resolved if resolved
+ end
+
def collect_payment_methods
Spree::Deprecation.warn('`Order#collect_payment_methods` is deprecated and will be removed in Spree 5.5. Use `collect_frontend_payment_methods` instead.')
diff --git a/spree/core/app/services/spree/carts/update.rb b/spree/core/app/services/spree/carts/update.rb
index 31675f965e..cc52efec2c 100644
--- a/spree/core/app/services/spree/carts/update.rb
+++ b/spree/core/app/services/spree/carts/update.rb
@@ -36,6 +36,7 @@ module Spree
def assign_cart_attributes
cart.email = params[:email] if params[:email].present?
cart.customer_note = params[:customer_note] if params.key?(:customer_note)
+
assign_market if params[:market_id].present?
cart.currency = params[:currency].upcase if params[:currency].present?
cart.locale = params[:locale] if params[:locale].present?
@@ -88,6 +89,7 @@ module Spree
def assign_market
market = cart.store.markets.find_by_prefix_id!(params[:market_id])
cart.market = market
+ cart.skip_market_resolution = true
end
# When the market changes, clear the shipping address if its country
diff --git a/spree/core/spec/services/spree/carts/update_spec.rb b/spree/core/spec/services/spree/carts/update_spec.rb
index 753881e336..890241ac27 100644
--- a/spree/core/spec/services/spree/carts/update_spec.rb
+++ b/spree/core/spec/services/spree/carts/update_spec.rb
@@ -71,6 +71,44 @@ module Spree
expect(order.reload.currency).to eq('USD')
end
end
+
+ context 'auto-switches market to match currency' do
+ let(:us_country) { Spree::Country.find_by(iso: 'US') || create(:country, iso: 'US') }
+ let(:de_country) { create(:country, iso: 'DE', name: 'Germany') }
+ let!(:us_market) { create(:market, store: store, countries: [us_country]) }
+ let!(:eu_market) { create(:market, :eu, store: store, countries: [de_country]) }
+ let(:order) { create(:order_with_line_items, user: user, store: store, market: us_market, currency: 'USD') }
+
+ it 'switches market when currency changes' do
+ result = described_class.call(cart: order, params: { currency: 'EUR' })
+
+ expect(result).to be_success
+ expect(order.reload.currency).to eq('EUR')
+ expect(order.market).to eq(eu_market)
+ end
+
+ it 'does not switch market when currency matches current market' do
+ result = described_class.call(cart: order, params: { currency: 'USD' })
+
+ expect(result).to be_success
+ expect(order.reload.market).to eq(us_market)
+ end
+
+ it 'does not switch market when market_id is explicitly provided' do
+ result = described_class.call(cart: order, params: { currency: 'EUR', market_id: us_market.prefixed_id })
+
+ expect(result).to be_success
+ expect(order.reload.market).to eq(us_market)
+ end
+
+ it 'returns failure when no market exists for currency' do
+ result = described_class.call(cart: order, params: { currency: 'GBP' })
+
+ expect(result).to be_failure
+ expect(order.reload.currency).to eq('USD')
+ expect(order.market).to eq(us_market)
+ end
+ end
end
describe 'updating market' do