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.

Expand shipping zones and markets to additional continents (#13898)

Damian Legawiec2bbbdd89c161
diff --git a/spree/core/app/services/spree/seeds/zones.rb b/spree/core/app/services/spree/seeds/zones.rb
index a24bc0cb7c..d98fe4d0cc 100644
--- a/spree/core/app/services/spree/seeds/zones.rb
+++ b/spree/core/app/services/spree/seeds/zones.rb
@@ -7,17 +7,23 @@ module Spree
eu_vat = Spree::Zone.where(name: 'EU_VAT', description: 'Countries that make up the EU VAT zone.', kind: 'country').first_or_create!
uk_vat = Spree::Zone.where(name: 'UK_VAT', kind: 'country').first_or_create!
north_america = Spree::Zone.where(name: 'North America', description: 'USA + Canada', kind: 'country').first_or_create!
- Spree::Zone.where(name: 'South America', description: 'South America', kind: 'country').first_or_create!
+ central_america_and_caribbean = Spree::Zone.where(name: 'Central America and Caribbean', description: 'Central America and Caribbean', kind: 'country').first_or_create!
+ south_america = Spree::Zone.where(name: 'South America', description: 'South America', kind: 'country').first_or_create!
middle_east = Spree::Zone.where(name: 'Middle East', description: 'Middle East', kind: 'country').first_or_create!
+ africa = Spree::Zone.where(name: 'Africa', description: 'Africa', kind: 'country').first_or_create!
asia = Spree::Zone.where(name: 'Asia', description: 'Asia', kind: 'country').first_or_create!
australia_and_oceania = Spree::Zone.where(name: 'Australia and Oceania', description: 'Australia and Oceania', kind: 'country').first_or_create!
create_zone_members(eu_vat, %w(PL FI PT RO DE FR SK HU SI IE AT ES IT BE SE LV BG LT CY LU MT DK NL EE HR CZ GR))
create_zone_members(north_america, %w(US CA))
+ create_zone_members(central_america_and_caribbean, %w(MX GT BZ SV HN NI CR PA CU DO HT JM BS BB TT PR AG DM GD KN LC VC AI AW BM KY CW GP MQ MS BL MF SX TC VG VI))
+ create_zone_members(south_america, %w(AR BO BR CL CO EC FK GF GY PY PE SR UY VE))
create_zone_members(middle_east, %w(BH CY EG IR IQ IL JO KW LB OM QA SA SY TR AE YE))
+ create_zone_members(africa, %w(DZ AO BJ BW BF BI CV CM CF TD KM CG CD CI DJ EG GQ ER SZ ET GA GM GH GN GW KE LS LR LY
+ MG MW ML MR MU YT MA MZ NA NE NG RE RW SH ST SN SC SL SO ZA SS SD TZ TG TN UG ZM ZW))
create_zone_members(asia, %w(AF AM AZ BH BD BT BN KH CN CX CC GE HK IN ID IR IQ IL JP JO KZ KW KG LA LB MO MY MV MN MM NP
KP OM PK PS PH QA SA SG KR LK SY TW TJ TH TR TM AE UZ VN YE))
- create_zone_members(australia_and_oceania, %w(AU NZ))
+ create_zone_members(australia_and_oceania, %w(AU NZ PG FJ SB VU NC PF WS AS GU KI MH FM NR NU NF MP PW PN TK TO TV WF CK))
uk_vat.zone_members.where(zoneable: Spree::Country.find_by(iso: 'GB')).first_or_create!
end
diff --git a/spree/core/db/sample_data/markets.rb b/spree/core/db/sample_data/markets.rb
index 29d92b1850..b2c76998d6 100644
--- a/spree/core/db/sample_data/markets.rb
+++ b/spree/core/db/sample_data/markets.rb
@@ -28,3 +28,37 @@ if eu_zone
eu_market.save!
end
end
+
+# Additional sample markets for the remaining continents. Each market pulls
+# its countries from the matching shipping zone so it only includes countries
+# with shipping coverage (and therefore valid market countries). Countries
+# already assigned to another market in the store are skipped, because
+# Spree::MarketCountry requires each country to belong to at most one market
+# per store.
+[
+ { name: 'South America', zone: 'South America', currency: 'USD', default_locale: 'es', supported_locales: 'es,pt' },
+ { name: 'Middle East', zone: 'Middle East', currency: 'USD', default_locale: 'en', supported_locales: 'en,ar' },
+ { name: 'Africa', zone: 'Africa', currency: 'USD', default_locale: 'en', supported_locales: 'en,fr,ar' },
+ { name: 'Asia', zone: 'Asia', currency: 'USD', default_locale: 'en', supported_locales: 'en' },
+ { name: 'Oceania', zone: 'Australia and Oceania', currency: 'AUD', default_locale: 'en', supported_locales: 'en' }
+].each do |attrs|
+ zone = Spree::Zone.find_by(name: attrs[:zone])
+ next unless zone
+
+ market = store.markets.find_or_initialize_by(name: attrs[:name])
+
+ assigned_scope = Spree::MarketCountry.joins(:market).
+ where(spree_markets: { store_id: store.id, deleted_at: nil })
+ assigned_scope = assigned_scope.where.not(market_id: market.id) if market.persisted?
+ assigned_country_ids = assigned_scope.pluck(:country_id).to_set
+
+ countries = zone.zone_members.where(zoneable_type: 'Spree::Country').map(&:zoneable).uniq.compact
+ countries = countries.reject { |c| assigned_country_ids.include?(c.id) }
+ next if countries.empty?
+
+ market.currency = attrs[:currency]
+ market.default_locale = attrs[:default_locale]
+ market.supported_locales = attrs[:supported_locales]
+ market.countries = countries
+ market.save!
+end
diff --git a/spree/core/db/sample_data/shipping_methods.rb b/spree/core/db/sample_data/shipping_methods.rb
index bae61a4548..c26a73353b 100644
--- a/spree/core/db/sample_data/shipping_methods.rb
+++ b/spree/core/db/sample_data/shipping_methods.rb
@@ -6,6 +6,12 @@ rescue ActiveRecord::RecordNotFound
end
europe_vat = Spree::Zone.find_by!(name: 'EU_VAT')
+central_america_and_caribbean = Spree::Zone.find_by(name: 'Central America and Caribbean')
+south_america = Spree::Zone.find_by(name: 'South America')
+middle_east = Spree::Zone.find_by(name: 'Middle East')
+africa = Spree::Zone.find_by(name: 'Africa')
+asia = Spree::Zone.find_by(name: 'Asia')
+australia_and_oceania = Spree::Zone.find_by(name: 'Australia and Oceania')
shipping_category = Spree::ShippingCategory.find_or_create_by!(name: 'Default')
shipping_methods = [
@@ -13,10 +19,18 @@ shipping_methods = [
{ name: 'UPS Two Day (USD)', zones: [north_america], display_on: 'both', shipping_categories: [shipping_category] },
{ name: 'UPS One Day (USD)', zones: [north_america], display_on: 'both', shipping_categories: [shipping_category] },
{ name: 'UPS Ground (EU)', zones: [europe_vat], display_on: 'both', shipping_categories: [shipping_category] },
- { name: 'UPS Ground (EUR)', zones: [europe_vat], display_on: 'both', shipping_categories: [shipping_category] }
+ { name: 'UPS Ground (EUR)', zones: [europe_vat], display_on: 'both', shipping_categories: [shipping_category] },
+ { name: 'DHL Standard (Central America and Caribbean)', zones: [central_america_and_caribbean].compact, display_on: 'both', shipping_categories: [shipping_category] },
+ { name: 'DHL Standard (South America)', zones: [south_america].compact, display_on: 'both', shipping_categories: [shipping_category] },
+ { name: 'DHL Standard (Middle East)', zones: [middle_east].compact, display_on: 'both', shipping_categories: [shipping_category] },
+ { name: 'DHL Standard (Africa)', zones: [africa].compact, display_on: 'both', shipping_categories: [shipping_category] },
+ { name: 'DHL Standard (Asia)', zones: [asia].compact, display_on: 'both', shipping_categories: [shipping_category] },
+ { name: 'DHL Standard (Australia and Oceania)', zones: [australia_and_oceania].compact, display_on: 'both', shipping_categories: [shipping_category] }
]
shipping_methods.each do |attributes|
+ next if attributes[:zones].empty?
+
Spree::ShippingMethod.where(name: attributes[:name]).first_or_create! do |shipping_method|
shipping_method.calculator = Spree::Calculator::Shipping::FlatRate.create!
shipping_method.zones = attributes[:zones]
@@ -30,9 +44,17 @@ end
'UPS Ground (EU)' => [5, 'USD'],
'UPS One Day (USD)' => [15, 'USD'],
'UPS Two Day (USD)' => [10, 'USD'],
- 'UPS Ground (EUR)' => [8, 'EUR']
+ 'UPS Ground (EUR)' => [8, 'EUR'],
+ 'DHL Standard (Central America and Caribbean)' => [15, 'USD'],
+ 'DHL Standard (South America)' => [20, 'USD'],
+ 'DHL Standard (Middle East)' => [20, 'USD'],
+ 'DHL Standard (Africa)' => [25, 'USD'],
+ 'DHL Standard (Asia)' => [20, 'USD'],
+ 'DHL Standard (Australia and Oceania)' => [25, 'USD']
}.each do |shipping_method_name, (price, currency)|
- shipping_method = Spree::ShippingMethod.find_by!(name: shipping_method_name)
+ shipping_method = Spree::ShippingMethod.find_by(name: shipping_method_name)
+ next unless shipping_method
+
shipping_method.calculator.preferences = { amount: price, currency: currency }
shipping_method.calculator.save!
shipping_method.save!