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.

Include `default_country` and `default_locale` in Countries API

Damian Legawiec4b63364ffc3b
diff --git a/api/app/serializers/spree/api/v3/country_serializer.rb b/api/app/serializers/spree/api/v3/country_serializer.rb
index 6de62b7d2e..a658ffdd50 100644
--- a/api/app/serializers/spree/api/v3/country_serializer.rb
+++ b/api/app/serializers/spree/api/v3/country_serializer.rb
@@ -7,9 +7,11 @@ module Spree
# ISO 3166-1 codes - iso is the identifier, no redundant id field
typelize iso: :string, iso3: :string, name: :string,
- states_required: :boolean, zipcode_required: :boolean
+ states_required: :boolean, zipcode_required: :boolean,
+ default_currency: 'string | null', default_locale: 'string | null'
- attributes :iso, :iso3, :name, :states_required, :zipcode_required
+ attributes :iso, :iso3, :name, :states_required, :zipcode_required,
+ :default_currency, :default_locale
many :states,
resource: Spree.api.state_serializer,
diff --git a/api/app/serializers/spree/api/v3/store_serializer.rb b/api/app/serializers/spree/api/v3/store_serializer.rb
index 4274a7fefe..f495f04d77 100644
--- a/api/app/serializers/spree/api/v3/store_serializer.rb
+++ b/api/app/serializers/spree/api/v3/store_serializer.rb
@@ -9,13 +9,17 @@ module Spree
customer_support_email: 'string | null', default_locale: :string,
supported_currencies: 'string[]', favicon_image_url: 'string | null',
logo_image_url: 'string | null', social_image_url: 'string | null',
- supported_locales: 'string[]'
+ supported_locales: 'string[]', default_country_iso: 'string | null'
attributes :name, :url, :meta_description, :meta_keywords, :seo_title,
:default_currency, :code, :default, :facebook, :twitter,
:instagram, :customer_support_email, :default_locale,
created_at: :iso8601, updated_at: :iso8601
+ attribute :default_country_iso do |store|
+ store.default_country&.iso
+ end
+
attribute :supported_currencies do |store|
store.supported_currencies_list.map(&:iso_code)
end
diff --git a/core/app/models/spree/country.rb b/core/app/models/spree/country.rb
index f5d7941479..3c01be4112 100644
--- a/core/app/models/spree/country.rb
+++ b/core/app/models/spree/country.rb
@@ -30,5 +30,23 @@ module Spree
def to_s
name
end
+
+ # Returns the default currency code for this country (e.g., 'USD', 'EUR')
+ # Uses the countries gem (ISO3166) for accurate currency data
+ def default_currency
+ iso3166_country&.currency_code
+ end
+
+ # Returns the default locale/language for this country (e.g., 'en', 'de')
+ # Uses the countries gem (ISO3166) for accurate language data
+ def default_locale
+ iso3166_country&.languages&.first
+ end
+
+ private
+
+ def iso3166_country
+ @iso3166_country ||= ISO3166::Country.new(iso)
+ end
end
end
diff --git a/core/spec/models/spree/country_spec.rb b/core/spec/models/spree/country_spec.rb
index 3d70494f16..744da35bbf 100644
--- a/core/spec/models/spree/country_spec.rb
+++ b/core/spec/models/spree/country_spec.rb
@@ -57,4 +57,36 @@ describe Spree::Country, type: :model do
end
end
end
+
+ describe '#default_currency' do
+ it 'returns the currency code from ISO3166' do
+ expect(america.default_currency).to eq('USD')
+ end
+
+ it 'returns EUR for Germany' do
+ germany = create(:country, name: 'Germany', iso_name: 'GERMANY', iso: 'DE', iso3: 'DEU', numcode: '276')
+ expect(germany.default_currency).to eq('EUR')
+ end
+
+ it 'returns nil for invalid ISO code' do
+ invalid_country = build(:country, iso: 'XX')
+ expect(invalid_country.default_currency).to be_nil
+ end
+ end
+
+ describe '#default_locale' do
+ it 'returns the primary language from ISO3166' do
+ expect(america.default_locale).to eq('en')
+ end
+
+ it 'returns de for Germany' do
+ germany = create(:country, name: 'Germany', iso_name: 'GERMANY', iso: 'DE', iso3: 'DEU', numcode: '276')
+ expect(germany.default_locale).to eq('de')
+ end
+
+ it 'returns nil for invalid ISO code' do
+ invalid_country = build(:country, iso: 'XX')
+ expect(invalid_country.default_locale).to be_nil
+ end
+ end
end
diff --git a/docs/api-reference/store.yaml b/docs/api-reference/store.yaml
index 3a792a8038..927e75552b 100644
--- a/docs/api-reference/store.yaml
+++ b/docs/api-reference/store.yaml
@@ -2703,6 +2703,14 @@ components:
type: boolean
zipcode_required:
type: boolean
+ default_currency:
+ type: string
+ nullable: true
+ description: Default currency code for this country (e.g., "USD", "EUR")
+ default_locale:
+ type: string
+ nullable: true
+ description: Default locale/language for this country (e.g., "en", "de")
states:
type: array
items:
@@ -3408,6 +3416,10 @@ components:
type: array
items:
type: string
+ default_country_iso:
+ type: string
+ nullable: true
+ description: ISO 3166-1 alpha-2 code of the store's default country (e.g., "US", "DE")
payment_methods:
type: array
items:
diff --git a/sdk/src/types/generated/StoreCountry.ts b/sdk/src/types/generated/StoreCountry.ts
index 5a7d963b1c..03de76fb70 100644
--- a/sdk/src/types/generated/StoreCountry.ts
+++ b/sdk/src/types/generated/StoreCountry.ts
@@ -1,4 +1,4 @@
-// Typelizer digest 9cec9b196b3857ffdab3e40ffd0ad55b
+// Typelizer digest 3ff3a67c633b5145e4a426806f7872f5
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {StoreState} from '@/types'
@@ -9,6 +9,8 @@ type StoreCountry = {
name: string;
states_required: boolean;
zipcode_required: boolean;
+ default_currency: string | null;
+ default_locale: string | null;
states?: Array<StoreState>;
}
diff --git a/sdk/src/types/generated/StoreStore.ts b/sdk/src/types/generated/StoreStore.ts
index e95ddd49d4..aeb0a88a82 100644
--- a/sdk/src/types/generated/StoreStore.ts
+++ b/sdk/src/types/generated/StoreStore.ts
@@ -1,4 +1,4 @@
-// Typelizer digest 606b4642ea4b931e163e9a1da19bffd0
+// Typelizer digest 1658e22712eb8843cecea630c217f790
//
// DO NOT MODIFY: This file was automatically generated by Typelizer.
import type {StorePaymentMethod} from '@/types'
@@ -20,6 +20,7 @@ type StoreStore = {
default_locale: string;
created_at: string;
updated_at: string;
+ default_country_iso: string | null;
supported_currencies: Array<string>;
favicon_image_url: string | null;
logo_image_url: string | null;