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
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.rbindex 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 doexpect(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')+ endendit 'auto-advances to payment after address submission' dodiff --git a/spree/core/app/models/concerns/spree/stores/markets.rb b/spree/core/app/models/concerns/spree/stores/markets.rbindex 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 Spreeendend- 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.rbindex 790fb48d9f..77d059bdd2 100644--- a/spree/core/app/models/spree/order.rb+++ b/spree/core/app/models/spree/order.rb@@ -182,6 +182,8 @@ module Spreealias_attribute :fulfillment_status, :shipment_statealias_attribute :payment_status, :payment_state+ delegate :has_markets?, to: :store, prefix: true+accepts_nested_attributes_for :line_itemsaccepts_nested_attributes_for :bill_addressaccepts_nested_attributes_for :ship_address@@ -195,10 +197,11 @@ module Spreebefore_validation :ensure_market_presencebefore_validation :ensure_currency_presencebefore_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_resolutionbefore_create :link_by_emailbefore_update :ensure_updated_shipments, :homogenize_line_item_currencies, if: :currency_changed?@@ -222,6 +225,7 @@ module Spreevalidates :shipment_total, MONEY_VALIDATIONvalidates :promo_total, NEGATIVE_MONEY_VALIDATIONvalidates :total, MONEY_VALIDATION+ validates :market, presence: true, if: :store_has_markets?validate :currency_must_be_supported_by_storevalidate :locale_must_be_supported_by_store@@ -453,7 +457,7 @@ module Spreeenddef ensure_market_presence- self.market ||= Spree::Current.market+ self.market ||= Spree::Current.market || store&.default_marketenddef allow_cancel?@@ -1056,6 +1060,16 @@ module Spreeendend+ # 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_methodsSpree::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.rbindex 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 Spreedef assign_cart_attributescart.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 Spreedef assign_marketmarket = cart.store.markets.find_by_prefix_id!(params[:market_id])cart.market = market+ cart.skip_market_resolution = trueend# When the market changes, clear the shipping address if its countrydiff --git a/spree/core/spec/services/spree/carts/update_spec.rb b/spree/core/spec/services/spree/carts/update_spec.rbindex 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 Spreeexpect(order.reload.currency).to eq('USD')endend++ 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+ endenddescribe 'updating market' do