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 how states import.

Matthew Kennedy6c238ba6fb69
diff --git a/core/app/models/spree/address.rb b/core/app/models/spree/address.rb
index a0e1037004..370876d32a 100644
--- a/core/app/models/spree/address.rb
+++ b/core/app/models/spree/address.rb
@@ -10,6 +10,12 @@ module Spree
'TO', 'TV', 'UG', 'AE', 'VU', 'YE', 'ZW'
].freeze
+ # The Information for this matches that used by PayPal, Stripe, and Shopify.
+ STATES_REQUIRED = [
+ '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
# those attributes depending of the logic of their applications
ADDRESS_FIELDS = %w(firstname lastname company address1 address2 city state zipcode country phone)
diff --git a/core/db/default/spree/countries.rb b/core/db/default/spree/countries.rb
index d4829eb099..e6cfe559e0 100644
--- a/core/db/default/spree/countries.rb
+++ b/core/db/default/spree/countries.rb
@@ -6,13 +6,14 @@ Carmen::Country.all.each do |country|
iso3: country.alpha_3_code,
iso: country.alpha_2_code,
iso_name: country.name.upcase,
- numcode: country.numeric_code,
- states_required: country.subregions?
+ numcode: country.numeric_code
).first_or_create
end
Spree::Config[:default_country_id] = Spree::Country.find_by(iso: 'US').id
-# find countries that do not use postal codes (by iso) and set 'zipcode_required' to false for them.
-
+# Find countries that do not use postal codes (by iso) and set 'zipcode_required' to false for them.
Spree::Country.where(iso: Spree::Address::NO_ZIPCODE_ISO_CODES).update_all(zipcode_required: false)
+
+# Find all countries that require a state (province) at checkout and set 'states_required' to true.
+Spree::Country.where(iso: Spree::Address::STATES_REQUIRED).update_all(states_required: true)
diff --git a/core/db/default/spree/states.rb b/core/db/default/spree/states.rb
index c11b65ca0c..20814ce365 100644
--- a/core/db/default/spree/states.rb
+++ b/core/db/default/spree/states.rb
@@ -3,10 +3,20 @@ Spree::Country.where(states_required: true).each do |country|
next unless carmen_country
carmen_country.subregions.each do |subregion|
- country.states.where(
- name: subregion.name,
- abbr: subregion.code,
- country_id: country.id
- ).first_or_create
+ 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
+ else
+ country.states.where(
+ name: subregion.name,
+ abbr: subregion.code,
+ country_id: country.id
+ ).first_or_create
+ end
end
end