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
Make Market <> Country relation easier to manage without the Zone in the middle
Damian Legawiecacdc564146b1
diff --git a/backend/db/schema.rb b/backend/db/schema.rbindex c55164a62c..b49677eed8 100644--- a/backend/db/schema.rb+++ b/backend/db/schema.rb@@ -10,7 +10,7 @@## It's strongly recommended that you check this file into your version control system.-ActiveRecord::Schema[8.1].define(version: 2026_02_20_151370) do+ActiveRecord::Schema[8.1].define(version: 2026_02_20_160786) do# These are extensions that must be enabled in order to support this databaseenable_extension "pg_catalog.plpgsql"enable_extension "pg_trgm"@@ -563,6 +563,16 @@ ActiveRecord::Schema[8.1].define(version: 2026_02_20_151370) dot.index ["source_id", "source_type"], name: "index_spree_log_entries_on_source_id_and_source_type"end+ create_table "spree_market_countries", force: :cascade do |t|+ t.bigint "country_id", null: false+ t.datetime "created_at", null: false+ t.bigint "market_id", null: false+ t.datetime "updated_at", null: false+ t.index ["country_id"], name: "index_spree_market_countries_on_country_id"+ t.index ["market_id", "country_id"], name: "index_spree_market_countries_on_market_id_and_country_id", unique: true+ t.index ["market_id"], name: "index_spree_market_countries_on_market_id"+ end+create_table "spree_markets", force: :cascade do |t|t.datetime "created_at", null: falset.string "currency", null: false@@ -575,13 +585,11 @@ ActiveRecord::Schema[8.1].define(version: 2026_02_20_151370) dot.string "supported_locales"t.boolean "tax_inclusive", default: false, null: falset.datetime "updated_at", null: false- t.bigint "zone_id", null: falset.index ["deleted_at"], name: "index_spree_markets_on_deleted_at"t.index ["store_id", "default"], name: "index_spree_markets_on_store_id_and_default", where: "(deleted_at IS NULL)"t.index ["store_id", "name"], name: "index_spree_markets_on_store_id_and_name", unique: true, where: "(deleted_at IS NULL)"t.index ["store_id", "position"], name: "index_spree_markets_on_store_id_and_position"t.index ["store_id"], name: "index_spree_markets_on_store_id"- t.index ["zone_id"], name: "index_spree_markets_on_zone_id"endcreate_table "spree_metafield_definitions", force: :cascade do |t|diff --git a/backend/engines/admin/app/controllers/spree/admin/markets_controller.rb b/backend/engines/admin/app/controllers/spree/admin/markets_controller.rbindex 0dc33839f3..c74f7c4281 100644--- a/backend/engines/admin/app/controllers/spree/admin/markets_controller.rb+++ b/backend/engines/admin/app/controllers/spree/admin/markets_controller.rb@@ -8,7 +8,7 @@ module Spreeprotecteddef load_data- @zones = Spree::Zone.order(:name)+ @countries = Spree::Country.order(:name)enddef permitted_resource_paramsdiff --git a/backend/engines/admin/app/javascript/spree/admin/controllers/select_controller.js b/backend/engines/admin/app/javascript/spree/admin/controllers/select_controller.jsindex fecaf1297b..969f003c08 100644--- a/backend/engines/admin/app/javascript/spree/admin/controllers/select_controller.js+++ b/backend/engines/admin/app/javascript/spree/admin/controllers/select_controller.js@@ -106,6 +106,10 @@ export default class extends Controller {if (this.multipleValue) {settings.maxItems = nullsettings.plugins = ['remove_button']+ settings.onItemAdd = function() {+ this.setTextboxValue('')+ this.refreshOptions(false)+ }if (this.activeOptionValue) settings.items = JSON.parse(this.activeOptionValue)} else {if (this.activeOptionValue) settings.items = [this.activeOptionValue]diff --git a/backend/engines/admin/app/views/spree/admin/markets/_form.html.erb b/backend/engines/admin/app/views/spree/admin/markets/_form.html.erbindex b68704f459..9cc8bdc36e 100644--- a/backend/engines/admin/app/views/spree/admin/markets/_form.html.erb+++ b/backend/engines/admin/app/views/spree/admin/markets/_form.html.erb@@ -8,7 +8,9 @@<div class="card-body"><%= f.spree_text_field :name, required: true %>- <%= f.spree_collection_select :zone_id, @zones, :id, :name, label: Spree.t(:zone), required: true %>+ <%= f.spree_collection_select :country_ids, @countries, :id, :name,+ { label: Spree.t(:countries), required: true, autocomplete: true },+ { multiple: true } %><%= f.spree_select :currency, currency_options(f.object.currency), { label: Spree.t(:currency), required: true, autocomplete: true } %>diff --git a/backend/engines/admin/spec/controllers/spree/admin/markets_controller_spec.rb b/backend/engines/admin/spec/controllers/spree/admin/markets_controller_spec.rbindex 436830379b..99b10b9b2e 100644--- a/backend/engines/admin/spec/controllers/spree/admin/markets_controller_spec.rb+++ b/backend/engines/admin/spec/controllers/spree/admin/markets_controller_spec.rb@@ -5,10 +5,10 @@ RSpec.describe Spree::Admin::MarketsController, type: :controller dostub_authorization!let(:store) { @default_store }- let(:zone) { create(:zone, kind: :country) }+ let(:country) { create(:country) }describe 'GET #index' do- let!(:market) { create(:market, store: store, zone: zone) }+ let!(:market) { create(:market, store: store, countries: [country]) }it 'returns a successful response' doget :index@@ -32,10 +32,10 @@ RSpec.describe Spree::Admin::MarketsController, type: :controller doexpect(assigns(:market)).to be_a_new(Spree::Market)end- it 'loads zones' do- zone+ it 'loads countries' do+ countryget :new- expect(assigns(:zones)).to include(zone)+ expect(assigns(:countries)).to include(country)endend@@ -44,7 +44,7 @@ RSpec.describe Spree::Admin::MarketsController, type: :controller do{market: {name: 'North America',- zone_id: zone.id,+ country_ids: [country.id],currency: 'USD',default_locale: 'en'}@@ -71,21 +71,21 @@ RSpec.describe Spree::Admin::MarketsController, type: :controller doenddescribe 'GET #edit' do- let(:market) { create(:market, store: store, zone: zone) }+ let(:market) { create(:market, store: store, countries: [country]) }it 'returns a successful response' doget :edit, params: { id: market.to_param }expect(response).to be_successfulend- it 'loads zones' do+ it 'loads countries' doget :edit, params: { id: market.to_param }- expect(assigns(:zones)).to include(zone)+ expect(assigns(:countries)).to include(country)endenddescribe 'PUT #update' do- let(:market) { create(:market, store: store, zone: zone) }+ let(:market) { create(:market, store: store, countries: [country]) }it 'updates the market' doput :update, params: { id: market.to_param, market: { name: 'Updated Name' } }@@ -106,7 +106,7 @@ RSpec.describe Spree::Admin::MarketsController, type: :controller doenddescribe 'DELETE #destroy' do- let!(:market) { create(:market, store: store, zone: zone) }+ let!(:market) { create(:market, store: store, countries: [country]) }it 'soft deletes the market' dodelete :destroy, params: { id: market.to_param }diff --git a/backend/engines/api/app/controllers/spree/api/v3/store/markets/countries_controller.rb b/backend/engines/api/app/controllers/spree/api/v3/store/markets/countries_controller.rbindex 03e736636b..46170e0a10 100644--- a/backend/engines/api/app/controllers/spree/api/v3/store/markets/countries_controller.rb+++ b/backend/engines/api/app/controllers/spree/api/v3/store/markets/countries_controller.rb@@ -5,7 +5,7 @@ module Spreemodule Marketsclass CountriesController < Store::BaseController# GET /api/v3/store/markets/:market_id/countries- # Returns countries from the market's zone (for checkout address dropdown)+ # Returns countries in the market (for checkout address dropdown)def indexcountries = market.countries.order(:name)diff --git a/backend/engines/api/app/controllers/spree/api/v3/store/markets_controller.rb b/backend/engines/api/app/controllers/spree/api/v3/store/markets_controller.rbindex cf64645128..fd2e3eb36f 100644--- a/backend/engines/api/app/controllers/spree/api/v3/store/markets_controller.rb+++ b/backend/engines/api/app/controllers/spree/api/v3/store/markets_controller.rb@@ -6,7 +6,7 @@ module Spree# GET /api/v3/store/markets# Returns all markets with nested countries (for country/currency switcher)def index- markets = current_store.markets.order(:position).includes(zone: { zone_members: :zoneable })+ markets = current_store.markets.order(:position).includes(:countries)render json: {data: markets.map { |market| serialize_market(market) }diff --git a/backend/engines/api/spec/integration/spree/api/v3/store/countries_spec.rb b/backend/engines/api/spec/integration/spree/api/v3/store/countries_spec.rbindex 26376d075b..567ed71039 100644--- a/backend/engines/api/spec/integration/spree/api/v3/store/countries_spec.rb+++ b/backend/engines/api/spec/integration/spree/api/v3/store/countries_spec.rb@@ -10,13 +10,7 @@ RSpec.describe 'Markets Countries API', type: :request, swagger_doc: 'api-referelet!(:new_york) { Spree::State.find_by(abbr: 'NY', country: usa) || create(:state, country: usa, name: 'New York', abbr: 'NY') }let!(:germany) { Spree::Country.find_by(iso: 'DE') || create(:country, iso: 'DE', name: 'Germany', states_required: false) }- let(:zone) do- zone = create(:zone, kind: :country)- zone.zone_members.create!(zoneable: usa)- zone.zone_members.create!(zoneable: germany)- zone- end- let!(:market) { create(:market, :default, store: store, zone: zone) }+ let!(:market) { create(:market, :default, store: store, countries: [usa, germany]) }let(:market_id) { market.prefixed_id }path '/api/v3/store/markets/{market_id}/countries' do@@ -24,7 +18,7 @@ RSpec.describe 'Markets Countries API', type: :request, swagger_doc: 'api-referetags 'Countries'produces 'application/json'security [api_key: []]- description 'Returns countries available in the market zone (for checkout address dropdown)'+ description 'Returns countries available in the market (for checkout address dropdown)'parameter name: 'x-spree-api-key', in: :header, type: :string, required: trueparameter name: :market_id, in: :path, type: :string, required: true,diff --git a/backend/engines/api/spec/integration/spree/api/v3/store/markets_spec.rb b/backend/engines/api/spec/integration/spree/api/v3/store/markets_spec.rbindex 850317058a..6f7ed5985d 100644--- a/backend/engines/api/spec/integration/spree/api/v3/store/markets_spec.rb+++ b/backend/engines/api/spec/integration/spree/api/v3/store/markets_spec.rb@@ -8,19 +8,8 @@ RSpec.describe 'Markets API', type: :request, swagger_doc: 'api-reference/store.let!(:usa) { Spree::Country.find_by(iso: 'US') || create(:country, iso: 'US', name: 'United States') }let!(:germany) { Spree::Country.find_by(iso: 'DE') || create(:country, iso: 'DE', name: 'Germany') }- let(:na_zone) do- zone = create(:zone, name: 'North America', kind: :country)- zone.zone_members.create!(zoneable: usa)- zone- end- let(:eu_zone) do- zone = create(:zone, name: 'Europe', kind: :country)- zone.zone_members.create!(zoneable: germany)- zone- end-- let!(:na_market) { create(:market, :default, name: 'North America', store: store, zone: na_zone, currency: 'USD', default_locale: 'en') }- let!(:eu_market) { create(:market, name: 'Europe', store: store, zone: eu_zone, currency: 'EUR', default_locale: 'de') }+ let!(:na_market) { create(:market, :default, name: 'North America', store: store, countries: [usa], currency: 'USD', default_locale: 'en') }+ let!(:eu_market) { create(:market, name: 'Europe', store: store, countries: [germany], currency: 'EUR', default_locale: 'de') }path '/api/v3/store/markets' doget 'List all markets' dodiff --git a/backend/engines/core/app/models/spree/current.rb b/backend/engines/core/app/models/spree/current.rbindex 883de1c510..999ffcba52 100644--- a/backend/engines/core/app/models/spree/current.rb+++ b/backend/engines/core/app/models/spree/current.rb@@ -15,7 +15,7 @@ module Spreeenddef zone- super || Spree::Zone.default_tax || market&.zone || store&.checkout_zone+ super || Spree::Zone.default_tax || store&.checkout_zoneenddef price_listsdiff --git a/backend/engines/core/app/models/spree/market.rb b/backend/engines/core/app/models/spree/market.rbindex d20f4d2ae0..4a44b4b803 100644--- a/backend/engines/core/app/models/spree/market.rb+++ b/backend/engines/core/app/models/spree/market.rb@@ -11,15 +11,17 @@ module Spree# Associations#belongs_to :store, class_name: 'Spree::Store'- belongs_to :zone, class_name: 'Spree::Zone'+ has_many :market_countries, class_name: 'Spree::MarketCountry', dependent: :destroy+ has_many :countries, through: :market_countries, class_name: 'Spree::Country'## Validations#- validates :store, :zone, presence: true+ validates :store, presence: truevalidates :name, presence: true, uniqueness: { scope: spree_base_uniqueness_scope + [:store_id] }validates :currency, presence: truevalidates :default_locale, presence: true+ validates :countries, presence: true## Callbacks@@ -32,7 +34,6 @@ module Spreescope :default, -> { where(default: true) }# Find the market that contains the given country for a store- # Joins through zone → zone_members to match country-based or state-based zones## @param country [Spree::Country] the country to look up# @param store [Spree::Store] the store to scope to@@ -40,18 +41,9 @@ module Spreedef self.for_country(country, store:)return nil unless country && store- joins(zone: :zone_members)+ joins(:market_countries).where(store_id: store.id)- .where(- spree_zone_members: { zoneable_type: 'Spree::Country', zoneable_id: country.id }- )- .or(- joins(zone: :zone_members)- .where(store_id: store.id)- .where(- spree_zone_members: { zoneable_type: 'Spree::State', zoneable_id: country.states.select(:id) }- )- )+ .where(spree_market_countries: { country_id: country.id }).order(:position).firstend@@ -66,13 +58,6 @@ module Spreestore.markets.default.first || store.markets.order(:position).firstend- # Returns the countries available in this market's zone- #- # @return [ActiveRecord::Relation<Spree::Country>]- def countries- zone.country_list- end-# Returns supported locales as an array, always including default_locale## @return [Array<String>]diff --git a/backend/engines/core/app/models/spree/market_country.rb b/backend/engines/core/app/models/spree/market_country.rbnew file mode 100644index 0000000000..5553c7761e--- /dev/null+++ b/backend/engines/core/app/models/spree/market_country.rb@@ -0,0 +1,11 @@+module Spree+ class MarketCountry < Spree.base_class+ self.table_name = 'spree_market_countries'++ belongs_to :market, class_name: 'Spree::Market'+ belongs_to :country, class_name: 'Spree::Country'++ validates :market, :country, presence: true+ validates :country_id, uniqueness: { scope: :market_id }+ end+enddiff --git a/backend/engines/core/app/services/spree/seeds/markets.rb b/backend/engines/core/app/services/spree/seeds/markets.rbindex 127c6702c1..13d2ba3029 100644--- a/backend/engines/core/app/services/spree/seeds/markets.rb+++ b/backend/engines/core/app/services/spree/seeds/markets.rb@@ -7,15 +7,19 @@ module SpreeSpree::Store.all.each do |store|next if store.markets.any?- zone = store.checkout_zone || Spree::Zone.find_by(name: 'North America') || Spree::Zone.first- next unless zone+ countries = if store.checkout_zone.present?+ store.checkout_zone.country_list+ else+ Spree::Country.all+ end+ next unless countries.any?store.markets.create!(name: 'Default',currency: store.default_currency,default_locale: store.default_locale || I18n.locale.to_s,- zone: zone,- default: true+ default: true,+ countries: countries)endenddiff --git a/backend/engines/core/db/migrate/20260220000000_create_spree_markets.rb b/backend/engines/core/db/migrate/20260220000000_create_spree_markets.rbindex 4f535d88e7..c8432a7af2 100644--- a/backend/engines/core/db/migrate/20260220000000_create_spree_markets.rb+++ b/backend/engines/core/db/migrate/20260220000000_create_spree_markets.rb@@ -4,7 +4,6 @@ class CreateSpreeMarkets < ActiveRecord::Migration[7.2]t.references :store, null: false, foreign_key: false, index: truet.string :name, null: falset.string :currency, null: false- t.references :zone, null: false, foreign_key: falset.string :default_locale, null: falset.string :supported_localest.boolean :tax_inclusive, null: false, default: false@@ -18,5 +17,13 @@ class CreateSpreeMarkets < ActiveRecord::Migration[7.2]add_index :spree_markets, [:store_id, :default], where: 'deleted_at IS NULL'add_index :spree_markets, [:store_id, :position]add_index :spree_markets, :deleted_at++ create_table :spree_market_countries do |t|+ t.references :market, null: false, foreign_key: false+ t.references :country, null: false, foreign_key: false+ t.timestamps+ end++ add_index :spree_market_countries, [:market_id, :country_id], unique: trueendenddiff --git a/backend/engines/core/lib/spree/permitted_attributes.rb b/backend/engines/core/lib/spree/permitted_attributes.rbindex bf45eec53b..1e61a8d359 100644--- a/backend/engines/core/lib/spree/permitted_attributes.rb+++ b/backend/engines/core/lib/spree/permitted_attributes.rb@@ -142,8 +142,8 @@ module Spree@@line_item_attributes = [:id, :variant_id, :quantity]- @@market_attributes = [:name, :currency, :zone_id, :default_locale, :supported_locales,- :tax_inclusive, :default, :position]+ @@market_attributes = [:name, :currency, :default_locale, :supported_locales,+ :tax_inclusive, :default, :position, country_ids: []]@@menu_attributes = [:name, :locale, :location]diff --git a/backend/engines/core/lib/spree/testing_support/factories/market_factory.rb b/backend/engines/core/lib/spree/testing_support/factories/market_factory.rbindex 7eb50414b4..2a7552354e 100644--- a/backend/engines/core/lib/spree/testing_support/factories/market_factory.rb+++ b/backend/engines/core/lib/spree/testing_support/factories/market_factory.rb@@ -4,7 +4,7 @@ FactoryBot.define docurrency { 'USD' }default_locale { 'en' }store- zone { association :zone_with_country }+ countries { [association(:country)] }trait :default dodefault { true }diff --git a/backend/engines/core/spec/models/spree/market_spec.rb b/backend/engines/core/spec/models/spree/market_spec.rbindex b950f5af40..0b901eac11 100644--- a/backend/engines/core/spec/models/spree/market_spec.rb+++ b/backend/engines/core/spec/models/spree/market_spec.rb@@ -7,11 +7,16 @@ RSpec.describe Spree::Market, type: :model dosubject { build(:market, store: store) }it { is_expected.to validate_presence_of(:store) }- it { is_expected.to validate_presence_of(:zone) }it { is_expected.to validate_presence_of(:name) }it { is_expected.to validate_presence_of(:currency) }it { is_expected.to validate_presence_of(:default_locale) }+ it 'validates presence of countries' do+ market = build(:market, store: store, countries: [])+ expect(market).not_to be_valid+ expect(market.errors[:countries]).to be_present+ end+it 'validates uniqueness of name scoped to store' docreate(:market, name: 'North America', store: store)market = build(:market, name: 'North America', store: store)@@ -20,23 +25,33 @@ RSpec.describe Spree::Market, type: :model doendend- describe '.for_country' do- let(:country) { create(:country) }- let(:zone) { create(:zone, kind: :country) }+ describe 'associations' do+ it 'has many countries through market_countries' do+ country1 = create(:country)+ country2 = create(:country)+ market = create(:market, store: store, countries: [country1, country2])- before do- zone.zone_members.create!(zoneable: country)+ expect(market.countries).to contain_exactly(country1, country2)end- it 'finds market by country through zone' do- market = create(:market, store: store, zone: zone)+ it 'destroys market_countries on destroy' do+ market = create(:market, store: store)+ expect { market.destroy }.to change(Spree::MarketCountry, :count).by(-1)+ end+ end++ describe '.for_country' do+ let(:country) { create(:country) }++ it 'finds market by country' do+ market = create(:market, store: store, countries: [country])result = described_class.for_country(country, store: store)expect(result).to eq(market)endit 'returns nil when no market matches' doother_country = create(:country)- create(:market, store: store, zone: zone)+ create(:market, store: store, countries: [country])result = described_class.for_country(other_country, store: store)expect(result).to be_nilend@@ -64,14 +79,6 @@ RSpec.describe Spree::Market, type: :model doendend- describe '#countries' do- it 'delegates to zone.country_list' do- zone = create(:zone_with_country)- market = build(:market, zone: zone)- expect(market.countries).to eq(zone.country_list)- end- end-describe '#supported_locales_list' doit 'returns default_locale when supported_locales is blank' domarket = build(:market, default_locale: 'en', supported_locales: nil)diff --git a/backend/engines/core/spec/models/spree/store_spec.rb b/backend/engines/core/spec/models/spree/store_spec.rbindex 1609423a22..97d1338e7d 100644--- a/backend/engines/core/spec/models/spree/store_spec.rb+++ b/backend/engines/core/spec/models/spree/store_spec.rb@@ -398,20 +398,12 @@ describe Spree::Store, type: :model, without_global_store: true dosubject { create(:store) }context 'with markets' do- let(:zone1) do- create(:zone, kind: 'country').tap do |z|- z.members.create(zoneable: create(:country, name: 'Germany', iso: 'DE'))- end- end- let(:zone2) do- create(:zone, kind: 'country').tap do |z|- z.members.create(zoneable: create(:country, name: 'France', iso: 'FR'))- end- end+ let!(:germany) { create(:country, name: 'Germany', iso: 'DE') }+ let!(:france) { create(:country, name: 'France', iso: 'FR') }before do- create(:market, store: subject, zone: zone1, currency: 'EUR', default: true)- create(:market, store: subject, zone: zone2, currency: 'EUR')+ create(:market, store: subject, countries: [germany], currency: 'EUR', default: true)+ create(:market, store: subject, countries: [france], currency: 'EUR')endit 'returns countries from all markets' do@@ -684,12 +676,7 @@ describe Spree::Store, type: :model, without_global_store: true dodescribe '#market_for_country' dolet!(:store) { create(:store) }let!(:country) { create(:country) }- let!(:zone) do- create(:zone, kind: 'country').tap do |z|- z.members.create(zoneable: country)- end- end- let!(:market) { create(:market, store: store, zone: zone) }+ let!(:market) { create(:market, store: store, countries: [country]) }it 'returns the market containing the country' doexpect(store.market_for_country(country)).to eq(market)