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.

Extend `Zone.match` to accept country and use market zone as `Current#zone`

Damian Legawiec545121dcac78
diff --git a/spree/core/app/models/spree/current.rb b/spree/core/app/models/spree/current.rb
index e207733822..8bad732321 100644
--- a/spree/core/app/models/spree/current.rb
+++ b/spree/core/app/models/spree/current.rb
@@ -34,10 +34,11 @@ module Spree
super || market&.default_locale || store&.default_locale
end
- # Returns the current tax zone, falling back to the default tax zone.
+ # Returns the current tax zone.
+ # Fallback: market's tax zone (from default country) -> global default tax zone.
# @return [Spree::Zone, nil]
def zone
- super || default_tax_zone
+ super || market&.tax_zone || default_tax_zone
end
# Returns the default tax zone (memoized per request).
diff --git a/spree/core/app/models/spree/market.rb b/spree/core/app/models/spree/market.rb
index 97b9686444..f185a1cc60 100644
--- a/spree/core/app/models/spree/market.rb
+++ b/spree/core/app/models/spree/market.rb
@@ -64,6 +64,14 @@ module Spree
countries.order(:name).first
end
+ # Returns the tax zone matching this market's default country.
+ # Used by Spree::Current to determine the browsing tax zone before a customer enters an address.
+ #
+ # @return [Spree::Zone, nil]
+ def tax_zone
+ @tax_zone ||= Spree::Zone.match(default_country)
+ end
+
# Returns supported locales as an array, always including default_locale
#
# @return [Array<String>]
diff --git a/spree/core/app/models/spree/zone.rb b/spree/core/app/models/spree/zone.rb
index c1734ef66e..1e3039114f 100644
--- a/spree/core/app/models/spree/zone.rb
+++ b/spree/core/app/models/spree/zone.rb
@@ -52,15 +52,27 @@ module Spree
# Returns the matching zone with the highest priority zone type (State, Country, Zone.)
# Returns nil in the case of no matches.
- def self.match(address)
- return unless address &&
- matches = includes(:zone_members).
- order('spree_zones.zone_members_count', 'spree_zones.created_at').
- where("(spree_zone_members.zoneable_type = 'Spree::Country' AND " \
- 'spree_zone_members.zoneable_id = ?) OR ' \
- "(spree_zone_members.zoneable_type = 'Spree::State' AND " \
- 'spree_zone_members.zoneable_id = ?)', address.country_id, address.state_id).
- references(:zones)
+ # Accepts either an address (with country_id/state_id) or a Spree::Country directly.
+ def self.match(address_or_country)
+ return unless address_or_country
+
+ if address_or_country.is_a?(Spree::Country)
+ country_id = address_or_country.id
+ state_id = nil
+ else
+ country_id = address_or_country.country_id
+ state_id = address_or_country.state_id
+ end
+
+ matches = includes(:zone_members).
+ order('spree_zones.zone_members_count', 'spree_zones.created_at').
+ where("(spree_zone_members.zoneable_type = 'Spree::Country' AND " \
+ 'spree_zone_members.zoneable_id = ?) OR ' \
+ "(spree_zone_members.zoneable_type = 'Spree::State' AND " \
+ 'spree_zone_members.zoneable_id = ?)', country_id, state_id).
+ references(:zones)
+
+ return if matches.empty?
%w[state country].each do |zone_kind|
if match = matches.detect { |zone| zone_kind == zone.kind }