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.

Add support for Currency/Locale headers in API

Damian Legawieca0332adda72d
diff --git a/api/app/controllers/concerns/spree/api/v3/http_caching.rb b/api/app/controllers/concerns/spree/api/v3/http_caching.rb
index 98815d26f1..723f2e6289 100644
--- a/api/app/controllers/concerns/spree/api/v3/http_caching.rb
+++ b/api/app/controllers/concerns/spree/api/v3/http_caching.rb
@@ -25,7 +25,7 @@ module Spree
# Set Vary headers to ensure proper CDN caching by currency/locale
def set_vary_headers
if guest_user?
- response.headers['Vary'] = 'Accept, X-Spree-Currency, X-Spree-Locale'
+ response.headers['Vary'] = 'Accept, x-spree-currency, x-spree-locale'
else
response.headers['Cache-Control'] = 'private, no-store'
end
diff --git a/api/app/controllers/concerns/spree/api/v3/locale_and_currency.rb b/api/app/controllers/concerns/spree/api/v3/locale_and_currency.rb
new file mode 100644
index 0000000000..bafee6d4cd
--- /dev/null
+++ b/api/app/controllers/concerns/spree/api/v3/locale_and_currency.rb
@@ -0,0 +1,59 @@
+module Spree
+ module Api
+ module V3
+ module LocaleAndCurrency
+ extend ActiveSupport::Concern
+
+ included do
+ before_action :set_locale_from_header
+ before_action :set_currency_from_header
+ end
+
+ protected
+
+ # Override current_locale to check header first
+ def current_locale
+ @current_locale ||= begin
+ locale = locale_from_header || locale_from_params || default_locale
+ locale.to_s if supported_locale?(locale)
+ end || default_locale
+ end
+
+ # Override current_currency to check header first
+ def current_currency
+ @current_currency ||= begin
+ currency = currency_from_header || currency_from_params || current_store&.default_currency
+ currency = currency&.upcase
+ supported_currency?(currency) ? currency : current_store&.default_currency
+ end
+ end
+
+ private
+
+ def set_locale_from_header
+ I18n.locale = current_locale
+ end
+
+ def set_currency_from_header
+ Spree::Current.currency = current_currency
+ end
+
+ def locale_from_header
+ request.headers['x-spree-locale'].presence
+ end
+
+ def currency_from_header
+ request.headers['x-spree-currency'].presence
+ end
+
+ def locale_from_params
+ params[:locale].presence
+ end
+
+ def currency_from_params
+ params[:currency].presence
+ end
+ end
+ end
+ end
+end
diff --git a/api/app/controllers/spree/api/v3/base_controller.rb b/api/app/controllers/spree/api/v3/base_controller.rb
index c21b39c251..4dc3cb95bb 100644
--- a/api/app/controllers/spree/api/v3/base_controller.rb
+++ b/api/app/controllers/spree/api/v3/base_controller.rb
@@ -8,6 +8,7 @@ module Spree
include Spree::Core::ControllerHelpers::Store
include Spree::Core::ControllerHelpers::Locale
include Spree::Core::ControllerHelpers::Currency
+ include Spree::Api::V3::LocaleAndCurrency
include Spree::Api::V3::JwtAuthentication
include Spree::Api::V3::ApiKeyAuthentication
include Spree::Api::V3::ErrorHandler
diff --git a/core/db/migrate/20260124000000_add_prefix_id_to_spree_tables.rb b/core/db/migrate/20260124000000_add_prefix_id_to_spree_tables.rb
index b4f43a3861..0cce692d79 100644
--- a/core/db/migrate/20260124000000_add_prefix_id_to_spree_tables.rb
+++ b/core/db/migrate/20260124000000_add_prefix_id_to_spree_tables.rb
@@ -9,7 +9,6 @@ class AddPrefixIdToSpreeTables < ActiveRecord::Migration[7.2]
spree_api_keys
spree_assets
spree_calculators
- spree_countries
spree_coupon_codes
spree_credit_cards
spree_custom_domains
@@ -69,7 +68,6 @@ class AddPrefixIdToSpreeTables < ActiveRecord::Migration[7.2]
spree_shipping_methods
spree_shipping_rates
spree_state_changes
- spree_states
spree_stock_items
spree_stock_locations
spree_stock_movements