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.

Fix store setup with different countries/currencies than US/USd (#14501)

Damian Legawiecef7f5c047d48
diff --git a/spree/api/spec/controllers/spree/api/v3/admin/setup_controller_spec.rb b/spree/api/spec/controllers/spree/api/v3/admin/setup_controller_spec.rb
index fd7c5d754d..dcb73d28f4 100644
--- a/spree/api/spec/controllers/spree/api/v3/admin/setup_controller_spec.rb
+++ b/spree/api/spec/controllers/spree/api/v3/admin/setup_controller_spec.rb
@@ -193,6 +193,55 @@ RSpec.describe Spree::Api::V3::Admin::SetupController, type: :controller do
end
end
+ # The store is always provisioned for US by default; choosing a different
+ # country at setup must move everything, not just work on a bare store.
+ context 'when changing the country and currency' do
+ before do
+ Spree::Stores::ProvisionDefaults.call(store: @default_store, country: Spree::Country.by_iso('US'))
+ @default_store.stock_locations.find_by(default: true).update!(
+ address1: '417 Montgomery St', city: 'San Francisco',
+ state_code: 'CA', state_name: 'California', zipcode: '94104'
+ )
+ end
+
+ it 'moves the whole store to the newly chosen country and currency' do
+ post :create, params: valid_params.merge(country_code: 'GB'), as: :json
+
+ expect(response).to have_http_status(:ok)
+
+ store = @default_store.reload
+ expect(store.default_country_code).to eq('GB')
+ expect(store.default_currency).to eq('GBP')
+ expect(store.default_market.country_codes).to eq(['GB'])
+ expect(store.default_market.currency).to eq('GBP')
+
+ domestic = store.delivery_zones.find_by(name: 'Domestic')
+ international = store.delivery_zones.find_by(name: 'International')
+ expect(domestic.members.pluck(:country_code)).to eq(['GB'])
+ expect(international.members.where(country_code: 'US')).to exist
+ expect(international.members.where(country_code: 'GB')).not_to exist
+
+ expect(store.delivery_methods.find_by(name: 'Standard').calculator.preferred_currency).to eq('GBP')
+
+ location = store.stock_locations.find_by(default: true)
+ expect(location.country_code).to eq('GB')
+ expect(location.state_code).to be_nil
+ expect(location.state_name).to be_nil
+ expect(location.address1).to be_nil
+ end
+
+ it 'keeps the warehouse address when the chosen country matches it' do
+ post :create, params: valid_params, as: :json
+
+ expect(response).to have_http_status(:ok)
+
+ location = @default_store.reload.stock_locations.find_by(default: true)
+ expect(location.country_code).to eq('US')
+ expect(location.state_code).to eq('CA')
+ expect(location.address1).to eq('417 Montgomery St')
+ end
+ end
+
context 'with an invalid token' do
it 'returns 404 without creating anything' do
post :create, params: valid_params.merge(setup_token: 'wrong'), as: :json
diff --git a/spree/core/app/services/spree/seeds/zones.rb b/spree/core/app/services/spree/seeds/zones.rb
deleted file mode 100644
index 46001b81bf..0000000000
--- a/spree/core/app/services/spree/seeds/zones.rb
+++ /dev/null
@@ -1,36 +0,0 @@
-module Spree
- module Seeds
- # Seeds the legacy tax zones by name only.
- #
- # Zone members addressed countries by row id, and countries are reference
- # data rather than records in 6.0, so there are no ids to point at. The
- # zones are created empty and match nothing until the tax provider replaces
- # them (docs/plans/6.0-tax-provider.md Phase 5). Shipping geography is
- # created from the store's own country instead — see
- # Spree::Stores::ProvisionDefaults.
- class Zones
- prepend Spree::ServiceModule::Base
-
- ZONES = [
- { name: 'EU_VAT', description: 'Countries that make up the EU VAT zone.' },
- { name: 'UK_VAT', description: nil },
- { name: 'North America', description: 'USA + Canada' },
- { name: 'Central America and Caribbean', description: 'Central America and Caribbean' },
- { name: 'South America', description: 'South America' },
- { name: 'Middle East', description: 'Middle East' },
- { name: 'Africa', description: 'Africa' },
- { name: 'Asia', description: 'Asia' },
- { name: 'Australia and Oceania', description: 'Australia and Oceania' }
- ].freeze
-
- def call
- ZONES.each do |attributes|
- Spree::Zone.where(name: attributes[:name]).first_or_create! do |zone|
- zone.description = attributes[:description]
- zone.kind = 'country'
- end
- end
- end
- end
- end
-end
diff --git a/spree/core/app/services/spree/stores/provision_defaults.rb b/spree/core/app/services/spree/stores/provision_defaults.rb
index 702581e143..c9b4fddd80 100644
--- a/spree/core/app/services/spree/stores/provision_defaults.rb
+++ b/spree/core/app/services/spree/stores/provision_defaults.rb
@@ -125,6 +125,11 @@ module Spree
where(name: Spree.t(:default_stock_location_name)).first_or_initialize
location.propagate_all_variants = false if location.new_record?
location.country_code = country.iso
+ if location.persisted? && location.will_save_change_to_country_code?
+ location.assign_attributes(address1: nil, address2: nil, city: nil,
+ state_code: nil, state_name: nil,
+ zipcode: nil, phone: nil)
+ end
location.active = true
location.default = true
location.save!
diff --git a/spree/core/db/sample_data/fulfillment.rb b/spree/core/db/sample_data/fulfillment.rb
index 901d7ba5e1..4be06a9c99 100644
--- a/spree/core/db/sample_data/fulfillment.rb
+++ b/spree/core/db/sample_data/fulfillment.rb
@@ -1,7 +1,7 @@
-# The whole demo fulfillment setup in one place: a rate-ready origin, a
-# realistic parcel, and Shopify-style delivery methods on the Domestic and
-# International zones. The free international method showcases
-# delivery-method rules — free above a spend threshold, hidden below it.
+# The whole demo fulfillment setup in one place: a realistic parcel and
+# Shopify-style delivery methods on the Domestic and International zones.
+# The free international method showcases delivery-method rules — free
+# above a spend threshold, hidden below it.
store = Spree::Store.default
# The zones are created from the store's country at first-run setup, so an
@@ -17,26 +17,6 @@ if store.delivery_zones.where(name: %w[Domestic International]).count < 2
store.reload
end
-# Carrier rate providers (EasyPost) cannot quote without a complete origin
-# address — a country-only stock location makes every carrier method silently
-# disappear from checkout. Demo data only: the production seed deliberately
-# leaves the address blank rather than invent an origin real rates would be
-# quoted from.
-stock_location = store.stock_locations.find_by(default: true) || store.stock_locations.first
-
-# Any missing field leaves the origin unquotable, so completeness — not just a
-# blank street — decides whether the demo address applies.
-if stock_location&.country_code == 'US' &&
- [stock_location.address1, stock_location.city, stock_location.state_code, stock_location.zipcode].any?(&:blank?)
- stock_location.update!(
- address1: '417 Montgomery St',
- city: 'San Francisco',
- state_code: 'CA',
- zipcode: '94104',
- phone: '415-555-0100'
- )
-end
-
# A realistic shipping box (imperial units — the demo store is US): its weight
# rides on every parcel and its dimensions feed dimensional-weight pricing,
# without which carrier quotes under-price bulky-but-light items. Applied only