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.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Add UI for switching Locale / Currency in a single Store
Damian Legawiec26148bf0ba58
diff --git a/core/lib/spree/core/controller_helpers/currency.rb b/core/lib/spree/core/controller_helpers/currency.rbindex 8a4af7a89a..6a52b0651b 100644--- a/core/lib/spree/core/controller_helpers/currency.rb+++ b/core/lib/spree/core/controller_helpers/currency.rb@@ -12,8 +12,6 @@ module Spreeenddef current_currency- # session support was previously in SpreeMultiCurrency so we would like- # to keep it for now@current_currency ||= if defined?(session) && session.key?(:currency) && supported_currency?(session[:currency])session[:currency]elsif params[:currency].present? && supported_currency?(params[:currency])diff --git a/frontend/app/assets/javascripts/spree/frontend.js b/frontend/app/assets/javascripts/spree/frontend.jsindex 694b853718..d6f332c41e 100644--- a/frontend/app/assets/javascripts/spree/frontend.js+++ b/frontend/app/assets/javascripts/spree/frontend.js@@ -15,6 +15,8 @@//= require spree/frontend/api_tokens//= require spree/frontend/carousel-noconflict//= require spree/frontend/cart+//= require spree/frontend/locale+//= require spree/frontend/currency//= require spree/frontend/checkout//= require spree/frontend/checkout/address//= require spree/frontend/checkout/address_book@@ -47,3 +49,5 @@ Spree.routes.api_v2_storefront_cart_remove_coupon_code = Spree.pathFor('api/v2/sSpree.routes.product = function(id) { return Spree.pathFor('products/' + id) }Spree.routes.product_related = function(id) { return Spree.pathFor('products/' + id + '/related') }Spree.routes.product_carousel = function (taxonId) { return Spree.pathFor('product_carousel/' + taxonId) }+Spree.routes.set_locale = function(locale) { return Spree.pathFor('locale/set?switch_to_locale=' + locale) }+Spree.routes.set_currency = function(currency) { return Spree.pathFor('currency/set?switch_to_currency=' + currency) }diff --git a/frontend/app/assets/javascripts/spree/frontend/currency.js b/frontend/app/assets/javascripts/spree/frontend/currency.jsnew file mode 100644index 0000000000..51224b158b--- /dev/null+++ b/frontend/app/assets/javascripts/spree/frontend/currency.js@@ -0,0 +1,12 @@+Spree.ready(function ($) {+ var currencySelect = $('#currency-select select')++ if (currencySelect.length) {+ currencySelect.on('change', function () {+ currencySelect.attr('disabled')+ var selectedCurrency = currencySelect.val()++ window.location = Spree.routes.set_currency(selectedCurrency)+ })+ }+})diff --git a/frontend/app/assets/javascripts/spree/frontend/locale.js b/frontend/app/assets/javascripts/spree/frontend/locale.jsnew file mode 100644index 0000000000..4ddc81ecde--- /dev/null+++ b/frontend/app/assets/javascripts/spree/frontend/locale.js@@ -0,0 +1,12 @@+Spree.ready(function ($) {+ var localeSelect = $('#locale-select select')++ if (localeSelect.length) {+ localeSelect.on('change', function () {+ localeSelect.attr('disabled')+ var selectedLocale = localeSelect.val()++ window.location = Spree.routes.set_locale(selectedLocale)+ })+ }+})diff --git a/frontend/app/controllers/spree/currency_controller.rb b/frontend/app/controllers/spree/currency_controller.rbindex cab88b9b78..2f4d37e5d7 100644--- a/frontend/app/controllers/spree/currency_controller.rb+++ b/frontend/app/controllers/spree/currency_controller.rb@@ -1,17 +1,13 @@module Spreeclass CurrencyController < StoreControllerdef set- @currency = supported_currencies.find { |currency| currency.iso_code == params[:currency] }- # Make sure that we update the current order, so the currency change is reflected.- current_order&.update(currency: @currency.iso_code)- session[:currency] = params[:currency] if Spree::Config[:show_store_selector]- respond_to do |format|- format.json { render json: [email protected]? }- format.html do- # We want to go back to where we came from!- redirect_back_or_default(root_path)- end+ new_currency = params[:switch_to_currency]&.upcase++ if new_currency.present? && supported_currency?(new_currency)+ current_order&.update(currency: new_currency)+ session[:currency] = new_currencyend+ redirect_back_or_default(root_path(currency: new_currency))endendenddiff --git a/frontend/app/controllers/spree/locale_controller.rb b/frontend/app/controllers/spree/locale_controller.rbindex 882abbca0d..6a4aa7926d 100644--- a/frontend/app/controllers/spree/locale_controller.rb+++ b/frontend/app/controllers/spree/locale_controller.rb@@ -5,8 +5,12 @@ module Spreeenddef set- session[:locale] = params[:locale]- redirect_back_or_default(root_path(locale: current_locale))+ new_locale = params[:switch_to_locale] || params[:locale]++ if new_locale.present? && supported_locale?(new_locale)+ session[:locale] = new_locale+ end+ redirect_back_or_default(root_path(locale: new_locale))endendenddiff --git a/frontend/app/controllers/spree/store_controller.rb b/frontend/app/controllers/spree/store_controller.rbindex 16c5e9ea82..1febbb8a21 100644--- a/frontend/app/controllers/spree/store_controller.rb+++ b/frontend/app/controllers/spree/store_controller.rb@@ -1,6 +1,8 @@module Spreeclass StoreController < Spree::BaseControllerinclude Spree::Core::ControllerHelpers::Order+ helper 'spree/locale'+ helper 'spree/currency'skip_before_action :verify_authenticity_token, only: :ensure_cart, raise: falsediff --git a/frontend/app/views/spree/locale/index.html.erb b/frontend/app/views/spree/locale/index.html.erbindex 3eab0ab5ce..0c5834e2a8 100644--- a/frontend/app/views/spree/locale/index.html.erb+++ b/frontend/app/views/spree/locale/index.html.erb@@ -1,15 +1 @@-<% if current_store.supported_locales_list.size > 1 %>- <li id="locale-select" data-hook>- <%= form_tag spree.set_locale_path, class: 'navbar-form' do %>- <div class="form-group">- <label for="switch_to_locale" class="sr-only">- <%= Spree.t(:'i18n.language') %>- </label>- <%= select_tag(:switch_to_locale,- options_for_select(supported_locales_options, I18n.locale),- class: 'form-control') %>- <noscript><%= submit_tag %></noscript>- </div>- <% end %>- </li>-<% end %>+<%= render 'spree/shared/locale_dropdown' %>diff --git a/frontend/app/views/spree/shared/_currency_dropdown.html.erb b/frontend/app/views/spree/shared/_currency_dropdown.html.erbnew file mode 100644index 0000000000..0fd52e274c--- /dev/null+++ b/frontend/app/views/spree/shared/_currency_dropdown.html.erb@@ -0,0 +1,13 @@+<% if defined?(should_render_currency_dropdown?) && should_render_currency_dropdown? %>+ <%= form_tag spree.set_currency_path, method: :get, class: 'px-4 py-3 w-100', id: 'currency-select' do %>+ <div class="form-group">+ <label for="switch_to_currency" class="dropdown-header text-center">+ <%= Spree.t(:choose_currency) %>+ </label>+ <%= select_tag(:switch_to_currency,+ options_for_select(supported_currency_options, current_currency),+ class: 'form-control') %>+ <noscript><%= submit_tag %></noscript>+ </div>+ <% end %>+<% end %>diff --git a/frontend/app/views/spree/shared/_change_store.html.erb b/frontend/app/views/spree/shared/_internationalization_options.html.erbsimilarity index 52%rename from frontend/app/views/spree/shared/_change_store.html.erbrename to frontend/app/views/spree/shared/_internationalization_options.html.erbindex 868a07ade8..f70e3c74dc 100644--- a/frontend/app/views/spree/shared/_change_store.html.erb+++ b/frontend/app/views/spree/shared/_internationalization_options.html.erb@@ -1,15 +1,18 @@-<% if Spree::Config.show_store_selector && stores.size > 1 %>- <li class="d-none d-xl-block change-store">- <div class="dropdown navbar-right-dropdown">- <button id="stores-button" type="button" data-toggle="dropdown" class="navbar-right-dropdown-toggle" aria-label="<%= Spree.t('nav_bar.change_country') %>">- <%= icon(name: 'global',+<% globe_icon = icon(name: 'global',classes: 'd-none d-xl-inline-block',width: 41,height: 36) %>- <%= icon(name: 'arrow-down',+<% arrow_icon = icon(name: 'arrow-down',classes: 'd-inline-block d-xl-none',width: 15,height: 15) %>++<% if defined?(should_render_store_chooser?) && should_render_store_chooser? %>+ <li class="d-none d-xl-block change-store">+ <div class="dropdown navbar-right-dropdown">+ <button id="stores-button" type="button" data-toggle="dropdown" class="navbar-right-dropdown-toggle" aria-label="<%= Spree.t('nav_bar.change_country') %>">+ <%= globe_icon %>+ <%= arrow_icon %></button><div id="stores_list" class="dropdown-menu dropdown-menu-right text-right">@@ -22,4 +25,19 @@</div></div></li>+<% elsif defined?(should_render_internationalization_dropdown?) && should_render_internationalization_dropdown? %>+ <li class="d-none d-xl-block change-store">+ <div class="dropdown navbar-right-dropdown">+ <button id="internationalization-button" type="button" data-toggle="dropdown" class="navbar-right-dropdown-toggle" aria-label="<%= Spree.t(:internationalization) %>">+ <%= globe_icon %>+ <%= arrow_icon %>+ </button>++ <div id="internationalization-options" class="dropdown-menu dropdown-menu-right text-left">+ <%= render 'spree/shared/locale_dropdown' %>+ <div class="dropdown-divider"></div>+ <%= render 'spree/shared/currency_dropdown' %>+ </div>+ </div>+ </li><% end %>diff --git a/frontend/app/views/spree/shared/_locale_dropdown.html.erb b/frontend/app/views/spree/shared/_locale_dropdown.html.erbnew file mode 100644index 0000000000..e4ccb8f189--- /dev/null+++ b/frontend/app/views/spree/shared/_locale_dropdown.html.erb@@ -0,0 +1,13 @@+<% if should_render_locale_dropdown? %>+ <%= form_tag spree.set_locale_path, method: :get, class: 'px-4 py-3 w-100', id: 'locale-select' do %>+ <div class="form-group">+ <label for="switch_to_locale" class="dropdown-header text-center">+ <%= Spree.t('i18n.language') %>+ </label>+ <%= select_tag(:switch_to_locale,+ options_for_select(supported_locales_options, I18n.locale),+ class: 'form-control') %>+ <noscript><%= submit_tag %></noscript>+ </div>+ <% end %>+<% end %>diff --git a/frontend/app/views/spree/shared/_nav_bar.html.erb b/frontend/app/views/spree/shared/_nav_bar.html.erbindex 90d57b5347..0aa43ab2b7 100644--- a/frontend/app/views/spree/shared/_nav_bar.html.erb+++ b/frontend/app/views/spree/shared/_nav_bar.html.erb@@ -31,5 +31,5 @@<li id="link-to-cart"><%= render 'spree/shared/cart', class: 'd-inline-block cart-icon', size: 36, item_count: 0 %></li>- <%= render partial: 'spree/shared/change_store' %>+ <%= render partial: 'spree/shared/internationalization_options' %></ul>diff --git a/frontend/spec/features/cart_spec.rb b/frontend/spec/features/cart_spec.rbindex 28c3e6c1f6..3a02d7555b 100644--- a/frontend/spec/features/cart_spec.rb+++ b/frontend/spec/features/cart_spec.rb@@ -118,4 +118,27 @@ describe 'Cart', type: :feature, inaccessible: true, js: true doexpect(page).not_to have_content(order.total)endend++ context 'switching currency' do+ let!(:product) { create(:product) }++ before do+ create(:store, default: true, supported_currencies: 'USD,EUR,GBP')+ create(:price, variant: product.master, currency: 'EUR', amount: 16.00)+ create(:price, variant: product.master, currency: 'GBP', amount: 23.00)+ add_to_cart(product)+ end++ it 'will change order currency and recalulate prices' do+ expect(page).to have_text '$19.99'+ find('#header #internationalization-button').click+ select 'EUR', from: 'switch_to_currency'+ expect(page).to have_text '€16.00'+ expect(order.reload.currency).to eq('EUR')+ find('#header #internationalization-button').click+ select 'GBP', from: 'switch_to_currency'+ expect(page).to have_text '£23.00'+ expect(order.reload.currency).to eq('GBP')+ end+ endenddiff --git a/frontend/spec/features/locale_spec.rb b/frontend/spec/features/locale_spec.rbindex ee2f4afb7c..42ea56f633 100644--- a/frontend/spec/features/locale_spec.rb+++ b/frontend/spec/features/locale_spec.rb@@ -74,14 +74,13 @@ describe 'setting locale', type: :feature docontext 'via set_locale endpoint' dobefore dostore.update(default_locale: 'en', supported_locales: 'en,fr')+ visit spree.set_locale_path(switch_to_locale: 'fr')endafter doI18n.locale = 'en'end- before { visit spree.set_locale_path(locale: 'fr') }-it_behaves_like 'translates cart page'end@@ -98,4 +97,15 @@ describe 'setting locale', type: :feature doexpect(page).to have_select('switch_to_locale', with_options: ['English (US)', 'Français (FR)'])endend++ context 'via UI', :js do+ before do+ store.update(default_locale: 'en', supported_locales: 'en,fr')+ visit spree.cart_path+ find('#header #internationalization-button').click+ select 'Français (FR)', from: 'Language'+ end++ it_behaves_like 'translates cart page'+ endenddiff --git a/frontend/spec/features/order_prices_spec.rb b/frontend/spec/features/order_prices_spec.rbdeleted file mode 100644index 28ca5eb0bf..0000000000--- a/frontend/spec/features/order_prices_spec.rb+++ /dev/null@@ -1,25 +0,0 @@-require 'spec_helper'--xdescribe 'Order', type: :feature, js: true do- let!(:store) { create(:store, default: true) }- let!(:product) { create(:product) }-- before do- reset_spree_preferences do |config|- config.show_store_selector = true- end-- create(:price, variant: product.master, currency: 'EUR', amount: 16.00)- create(:price, variant: product.master, currency: 'GBP', amount: 23.00)- end-- context 'when existing in the cart' do- it 'changes its currency, if user switches the currency.' do- visit spree.product_path(product)- click_button 'Add To Cart'- expect(page).to have_css('#link-to-cart', text: '$19.99')- select 'EUR', from: 'currency'- expect(page).to have_css('#link-to-cart', text: '€16.00')- end- end-enddiff --git a/frontend/spec/features/product_prices_spec.rb b/frontend/spec/features/product_prices_spec.rbindex 35686247da..fcd86fa451 100644--- a/frontend/spec/features/product_prices_spec.rb+++ b/frontend/spec/features/product_prices_spec.rb@@ -1,15 +1,12 @@require 'spec_helper'describe 'Product with prices in multiple currencies', type: :feature, js: true do- xcontext 'currency switcher' do+ context 'currency switcher' docontext 'with USD, EUR and GBP as currencies' do- let!(:store) { create(:store, default: true) }+ let!(:store) { create(:store, default: true, supported_currencies: 'USD,EUR,GBP') }let!(:product) { create(:product) }before do- reset_spree_preferences do |config|- config.show_store_selector = true- endcreate(:price, variant: product.master, currency: 'EUR', amount: 16.00)create(:price, variant: product.master, currency: 'GBP', amount: 23.00)end@@ -17,29 +14,21 @@ describe 'Product with prices in multiple currencies', type: :feature, js: trueit 'can switch by currency', :js dovisit spree.product_path(product)expect(page).to have_text '$19.99'- select 'EUR', from: 'currency'+ find('#header #internationalization-button').click+ select 'EUR', from: 'switch_to_currency'expect(page).to have_text '€16.00'- select 'GBP', from: 'currency'+ find('#header #internationalization-button').click+ select 'GBP', from: 'switch_to_currency'expect(page).to have_text '£23.00'- end-- context 'and :show_store_selector is false' do- before do- reset_spree_preferences do |config|- config.show_store_selector = false- end- end-- it 'will not render the currency selector' do- visit spree.product_path(product)- expect(page).to have_current_path(spree.product_path(product))- expect(page).to_not have_text 'Currency'- end+ visit spree.products_path+ expect(page).to have_text '£23.00'+ find('#header #internationalization-button').click+ expect(page).to have_select('currency', selected: 'GBP')endendend- context 'store currency' do+ context 'store default currency' dolet!(:store) { create(:store, default: true, default_currency: 'GBP') }let(:product) { create(:product, price: 9.99, currency: 'USD') }