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 checkout countries and states suggestions based on current_store
Nathaniel Nande7bda591da551
diff --git a/api/app/controllers/spree/api/v2/storefront/countries_controller.rb b/api/app/controllers/spree/api/v2/storefront/countries_controller.rbindex c2389b0efd..0f6c180417 100644--- a/api/app/controllers/spree/api/v2/storefront/countries_controller.rb+++ b/api/app/controllers/spree/api/v2/storefront/countries_controller.rb@@ -24,10 +24,17 @@ module Spreeresource,include: resource_includes,fields: sparse_fields,- params: { include_states: true }+ params: resource_serializer_params).serializable_hashend+ def resource_serializer_params+ default_params = { include_states: true }+ default_params.merge!(current_store: current_store) if params[:include_checkout_zone_applicable_states]++ default_params+ end+def collectioncollection_finder.new(scope, params).callenddiff --git a/api/app/serializers/spree/v2/storefront/country_serializer.rb b/api/app/serializers/spree/v2/storefront/country_serializer.rbindex 30775fc4ef..e0d14f440e 100644--- a/api/app/serializers/spree/v2/storefront/country_serializer.rb+++ b/api/app/serializers/spree/v2/storefront/country_serializer.rb@@ -12,6 +12,12 @@ module Spreeendhas_many :states, if: proc { |_record, params| params && params[:include_states] }++ attribute :checkout_zone_applicable_states, if: proc { |_record, params| params && params[:current_store] } do |object, params|+ current_store = params[:current_store]++ current_store.states_available_for_checkout(object)+ endendendenddiff --git a/core/app/helpers/spree/base_helper.rb b/core/app/helpers/spree/base_helper.rbindex 4cca35fe23..cdeb95998e 100644--- a/core/app/helpers/spree/base_helper.rb+++ b/core/app/helpers/spree/base_helper.rb@@ -1,9 +1,7 @@module Spreemodule BaseHelperdef available_countries- checkout_zone = current_store.checkout_zone || Spree::Zone.default_checkout_zone-- countries = checkout_zone ? checkout_zone.country_list : Spree::Country.all+ countries = current_store.countries_available_for_checkoutcountries.collect do |country|country.name = Spree.t(country.iso, scope: 'country_names', default: country.name)diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rbindex ef6fe3764f..a1287a3261 100644--- a/core/app/models/spree/store.rb+++ b/core/app/models/spree/store.rb@@ -51,6 +51,18 @@ module Spree"#{name} (#{code})"end+ def countries_available_for_checkout+ checkout_zone_or_default.try(:country_list) || Spree::Country.all+ end++ def states_available_for_checkout(country)+ checkout_zone_or_default.try(:state_list_for, country) || country.states+ end++ def checkout_zone_or_default+ checkout_zone || Spree::Zone.default_checkout_zone+ end+privatedef ensure_default_exists_and_is_uniquediff --git a/core/app/models/spree/zone.rb b/core/app/models/spree/zone.rbindex 84f4597d6d..47f99d3747 100644--- a/core/app/models/spree/zone.rb+++ b/core/app/models/spree/zone.rb@@ -101,8 +101,6 @@ module Spreezone_member.zoneable_id == address.country_idwhen 'Spree::State'zone_member.zoneable_id == address.state_id- else- falseendendend@@ -114,8 +112,6 @@ module Spreezoneableswhen 'state' thenzoneables.collect(&:country)- else- []end.flatten.compact.uniqend@@ -171,6 +167,19 @@ module Spreetrueend+ def state_list+ case kind+ when 'country'+ zoneables.map(&:states)+ when 'state'+ zoneables+ end.flatten.compact.uniq+ end++ def state_list_for(country)+ state_list.select { |state| state.country == country }+ end+privatedef remove_defunct_membersdiff --git a/frontend/app/assets/javascripts/spree/frontend/checkout/address.js b/frontend/app/assets/javascripts/spree/frontend/checkout/address.jsindex f882d6fc9c..e58bc44714 100644--- a/frontend/app/assets/javascripts/spree/frontend/checkout/address.js+++ b/frontend/app/assets/javascripts/spree/frontend/checkout/address.js@@ -6,11 +6,11 @@ Spree.ready(function ($) {if (countryId != null) {if (Spree.Checkout[countryId] == null) {$.ajax({- async: false, method: 'GET', url: Spree.pathFor('/api/v2/storefront/countries/' + countryId + '?include=states'), dataType: 'json'+ async: false, method: 'GET', url: Spree.pathFor('/api/v2/storefront/countries/' + countryId + '?include=states&include_checkout_zone_applicable_states=true'), dataType: 'json'}).done(function (data) {- var json = data.included; var xStates = []+ var json = data.data.attributes.checkout_zone_applicable_states; var xStates = [];for (var i = 0; i < json.length; i++) {- var obj = json[i]; xStates.push({ 'id': obj.id, 'name': obj.attributes.name })+ var obj = json[i]; xStates.push({ 'id': obj.id, 'name': obj.name })}Spree.Checkout[countryId] = {states: xStates,diff --git a/frontend/app/helpers/spree/addresses_helper.rb b/frontend/app/helpers/spree/addresses_helper.rbindex 83aa4a0e00..59f19b7836 100644--- a/frontend/app/helpers/spree/addresses_helper.rb+++ b/frontend/app/helpers/spree/addresses_helper.rb@@ -41,7 +41,7 @@ module Spreecountry ||= Spree::Country.find(Spree::Config[:default_country_id])have_states = country.states.any?state_elements = [- form.collection_select(:state_id, country.states.order(:name),+ form.collection_select(:state_id, checkout_zone_applicable_states_for(country).sort_by(&:name),:id, :name,{ prompt: Spree.t(:state) },class: [have_states ? 'required' : 'hidden', 'spree-flat-select'].compact,@@ -69,5 +69,9 @@ module Spreetry_spree_current_user.addresses.where(country: available_countries)end++ def checkout_zone_applicable_states_for(country)+ current_store.states_available_for_checkout(country)+ endendend