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 currency param in URLs to bust HTTP cache

Damian Legawiecc86256c766c4
diff --git a/core/lib/spree/core/controller_helpers/currency.rb b/core/lib/spree/core/controller_helpers/currency.rb
index 91d471c829..c9599f6350 100644
--- a/core/lib/spree/core/controller_helpers/currency.rb
+++ b/core/lib/spree/core/controller_helpers/currency.rb
@@ -9,6 +9,7 @@ module Spree
helper_method :supported_currencies_for_all_stores
helper_method :current_currency
helper_method :supported_currency?
+ helper_method :currency_param
end
def current_currency
@@ -41,6 +42,12 @@ module Spree
supported_currencies.map(&:iso_code).include?(currency_iso_code.upcase)
end
+
+ def currency_param
+ return if current_currency == current_store.default_currency
+
+ current_currency
+ end
end
end
end
diff --git a/core/spec/lib/spree/core/controller_helpers/currency_spec.rb b/core/spec/lib/spree/core/controller_helpers/currency_spec.rb
index 6b8da11529..9e0fd37572 100644
--- a/core/spec/lib/spree/core/controller_helpers/currency_spec.rb
+++ b/core/spec/lib/spree/core/controller_helpers/currency_spec.rb
@@ -26,4 +26,20 @@ describe Spree::Core::ControllerHelpers::Currency, type: :controller do
expect(controller.supported_currencies).to include(::Money::Currency.find(currency))
end
end
+
+ describe '#currency_param' do
+ let!(:store) { create :store, default: true, default_currency: 'EUR', supported_currencies: 'EUR,GBP' }
+
+ context 'same as store default locale' do
+ before { controller.params = { currency: 'EUR' } }
+
+ it { expect(controller.currency_param).to eq(nil) }
+ end
+
+ context 'different than store locale' do
+ before { controller.params = { currency: 'GBP' } }
+
+ it { expect(controller.currency_param).to eq('GBP') }
+ end
+ end
end
diff --git a/frontend/app/controllers/concerns/spree/locale_urls.rb b/frontend/app/controllers/concerns/spree/locale_urls.rb
index dda4f9a7b2..5905d6bdaf 100644
--- a/frontend/app/controllers/concerns/spree/locale_urls.rb
+++ b/frontend/app/controllers/concerns/spree/locale_urls.rb
@@ -9,9 +9,7 @@ module Spree
private
def default_url_options
- return super if locale_param.nil?
-
- super.merge(locale: locale_param)
+ super.merge(locale: locale_param, currency: currency_param)
end
def redirect_to_default_locale
diff --git a/frontend/spec/features/product_prices_spec.rb b/frontend/spec/features/product_prices_spec.rb
index c187eadce5..78cb4dd374 100644
--- a/frontend/spec/features/product_prices_spec.rb
+++ b/frontend/spec/features/product_prices_spec.rb
@@ -20,6 +20,7 @@ describe 'Product with prices in multiple currencies', type: :feature, js: true
expect(page).to have_text '£23.00'
visit spree.products_path
expect(page).to have_text '£23.00'
+ expect(page).to have_link product.name, href: "/products/#{product.slug}?currency=GBP"
open_i18n_menu
expect(page).to have_select('switch_to_currency', selected: 'GBP')
end