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 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.rb
index 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 Spree
resource,
include: resource_includes,
fields: sparse_fields,
- params: { include_states: true }
+ params: resource_serializer_params
).serializable_hash
end
+ 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 collection
collection_finder.new(scope, params).call
end
diff --git a/api/app/serializers/spree/v2/storefront/country_serializer.rb b/api/app/serializers/spree/v2/storefront/country_serializer.rb
index 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 Spree
end
has_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)
+ end
end
end
end
diff --git a/core/app/helpers/spree/base_helper.rb b/core/app/helpers/spree/base_helper.rb
index 4cca35fe23..cdeb95998e 100644
--- a/core/app/helpers/spree/base_helper.rb
+++ b/core/app/helpers/spree/base_helper.rb
@@ -1,9 +1,7 @@
module Spree
module BaseHelper
def 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_checkout
countries.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.rb
index 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
+
private
def ensure_default_exists_and_is_unique
diff --git a/core/app/models/spree/zone.rb b/core/app/models/spree/zone.rb
index 84f4597d6d..47f99d3747 100644
--- a/core/app/models/spree/zone.rb
+++ b/core/app/models/spree/zone.rb
@@ -101,8 +101,6 @@ module Spree
zone_member.zoneable_id == address.country_id
when 'Spree::State'
zone_member.zoneable_id == address.state_id
- else
- false
end
end
end
@@ -114,8 +112,6 @@ module Spree
zoneables
when 'state' then
zoneables.collect(&:country)
- else
- []
end.flatten.compact.uniq
end
@@ -171,6 +167,19 @@ module Spree
true
end
+ 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
+
private
def remove_defunct_members
diff --git a/frontend/app/assets/javascripts/spree/frontend/checkout/address.js b/frontend/app/assets/javascripts/spree/frontend/checkout/address.js
index 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.rb
index 83aa4a0e00..59f19b7836 100644
--- a/frontend/app/helpers/spree/addresses_helper.rb
+++ b/frontend/app/helpers/spree/addresses_helper.rb
@@ -41,7 +41,7 @@ module Spree
country ||= 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 Spree
try_spree_current_user.addresses.where(country: available_countries)
end
+
+ def checkout_zone_applicable_states_for(country)
+ current_store.states_available_for_checkout(country)
+ end
end
end