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.

Update Carmen Data Import

Matthew Kennedy8b4079e2f5cd
diff --git a/core/app/models/spree/address.rb b/core/app/models/spree/address.rb
index 370876d32a..c98b55e7a1 100644
--- a/core/app/models/spree/address.rb
+++ b/core/app/models/spree/address.rb
@@ -10,10 +10,10 @@ module Spree
'TO', 'TV', 'UG', 'AE', 'VU', 'YE', 'ZW'
].freeze
- # The Information for this matches that used by PayPal, Stripe, and Shopify.
+ # The required states listed below match those used by PayPal and Shopify.
STATES_REQUIRED = [
- 'AU', 'AE', 'BR', 'CA', 'CN', 'ES', 'HK', 'IE', 'IN', 'IT', 'MY', 'MX', 'NZ', 'PT', 'RO',
- 'TH', 'US', 'ZA'
+ 'AU', 'AE', 'BR', 'CA', 'CN', 'ES', 'HK', 'IE', 'IN',
+ 'IT', 'MY', 'MX', 'NZ', 'PT', 'RO', 'TH', 'US', 'ZA'
].freeze
# we're not freezing this on purpose so developers can extend and manage
diff --git a/core/db/default/spree/countries.rb b/core/db/default/spree/countries.rb
index e6cfe559e0..bf6a76c901 100644
--- a/core/db/default/spree/countries.rb
+++ b/core/db/default/spree/countries.rb
@@ -1,6 +1,11 @@
require 'carmen'
+EXCLUDED_COUNTRIES = ['AQ', 'AX', 'GS', 'UM', 'HM', 'IO', 'EH', 'BV', 'TF'].freeze
+
Carmen::Country.all.each do |country|
+ # Skip the creation of some territories, uninhabited islands and the Antarctic.
+ next if EXCLUDED_COUNTRIES.include?(country.alpha_2_code)
+
Spree::Country.where(
name: country.name,
iso3: country.alpha_3_code,
diff --git a/core/db/default/spree/states.rb b/core/db/default/spree/states.rb
index 20814ce365..3b2d9fc4cd 100644
--- a/core/db/default/spree/states.rb
+++ b/core/db/default/spree/states.rb
@@ -1,22 +1,49 @@
+# frozen_string_literal: true
+
+EXCLUDED_US_STATES = ['UM', 'AS', 'MP', 'VI', 'PR', 'GU'].freeze
+EXCLUDED_CN_STATES = ['HK', 'MO', 'TW'].freeze
+
+def state_level(country, subregion)
+ country.states.where(
+ name: subregion.name,
+ abbr: subregion.code
+ ).first_or_create
+end
+
+def province_level(country, subregion)
+ subregion.subregions.each do |province|
+ country.states.where(
+ name: province.name,
+ abbr: province.code
+ ).first_or_create
+ end
+end
+
Spree::Country.where(states_required: true).each do |country|
carmen_country = Carmen::Country.named(country.name)
next unless carmen_country
carmen_country.subregions.each do |subregion|
- if subregion.subregions?
- subregion.subregions.each do |province|
- country.states.where(
- name: province.name,
- abbr: province.code,
- country_id: country.id
- ).first_or_create
- end
+ if carmen_country.alpha_2_code == 'US'
+ # Produces 50 states, one postal district (Washington DC)
+ # and 3 APO's as you would expect to see on any good U.S. states list.
+ next if EXCLUDED_US_STATES.include?(subregion.code)
+
+ state_level(country, subregion)
+ elsif carmen_country.alpha_2_code == 'CA' || carmen_country.alpha_2_code == 'MX'
+ # Force Canada and Mexico to use state-level data import from Carmen Gem
+ # else we pull in a subset of provinces that are not common at checkout.
+ state_level(country, subregion)
+ elsif carmen_country.alpha_2_code == 'CN'
+ # Removes 3 "States" from that list that are also listed as Countries,
+ # Hong Kong, Taiwan and Macao
+ next if EXCLUDED_CN_STATES.include?(subregion.code)
+
+ state_level(country, subregion)
+ elsif subregion.subregions?
+ province_level(country, subregion)
else
- country.states.where(
- name: subregion.name,
- abbr: subregion.code,
- country_id: country.id
- ).first_or_create
+ state_level(country, subregion)
end
end
end
diff --git a/core/db/default/spree/zones.rb b/core/db/default/spree/zones.rb
index 4df53e2de6..e32f14a9c8 100644
--- a/core/db/default/spree/zones.rb
+++ b/core/db/default/spree/zones.rb
@@ -16,7 +16,7 @@ end
middle_east.zone_members.where(zoneable: Spree::Country.find_by!(iso: name)).first_or_create!
end
-%w(AF AM AZ BH BD BT BN KH CN CX CC IO GE HK IN ID IR IQ IL JP JO KZ KW KG LA LB MO MY MV MN MM NP
+%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).each do |name|
asia.zone_members.where(zoneable: Spree::Country.find_by!(iso: name)).first_or_create!
end