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 Zone form section in Store edit/new form, add helper for selected zones

Przemek Kosakowski8552521097f5
diff --git a/backend/app/controllers/spree/admin/stores_controller.rb b/backend/app/controllers/spree/admin/stores_controller.rb
index 4976ee746c..30103e5456 100644
--- a/backend/app/controllers/spree/admin/stores_controller.rb
+++ b/backend/app/controllers/spree/admin/stores_controller.rb
@@ -7,6 +7,7 @@ module Spree
before_action :normalize_supported_currencies, only: [:create, :update]
before_action :set_default_country_id, only: :new
before_action :load_all_countries, only: [:new, :edit, :update, :create]
+ before_action :load_all_zones, only: %i[new edit]
if defined?(SpreeI18n)
include SpreeI18n::LocaleHelper
@@ -114,6 +115,10 @@ module Spree
@countries = Spree::Country.all
end
+ def load_all_zones
+ @zones = Spree::Zone.all
+ end
+
def set_default_currency
@store.default_currency = Spree::Config[:currency]
end
diff --git a/backend/app/helpers/spree/admin/stores_helper.rb b/backend/app/helpers/spree/admin/stores_helper.rb
new file mode 100644
index 0000000000..c99a988d41
--- /dev/null
+++ b/backend/app/helpers/spree/admin/stores_helper.rb
@@ -0,0 +1,18 @@
+module Spree
+ module Admin
+ module StoresHelper
+ def selected_checkout_zone(store)
+ return store.checkout_zone_id unless store.checkout_zone_id.nil?
+ return checkout_zone('No Limits').id if Spree::Config[:checkout_zone].nil?
+
+ return checkout_zone(Spree::Config[:checkout_zone]).id unless Spree::Config[:checkout_zone].nil?
+ end
+
+ private
+
+ def checkout_zone(name)
+ Spree::Zone.find_by(name: name)
+ end
+ end
+ end
+end
diff --git a/backend/app/views/spree/admin/stores/_form.html.erb b/backend/app/views/spree/admin/stores/_form.html.erb
index 56421f0676..722bf1b4a1 100644
--- a/backend/app/views/spree/admin/stores/_form.html.erb
+++ b/backend/app/views/spree/admin/stores/_form.html.erb
@@ -116,6 +116,12 @@
<%= f.select :supported_currencies, currency_options(@store.supported_currencies&.split(',')), { prompt: false }, { multiple: true, class: 'select2' } %>
<%= f.error_message_on :supported_currencies %>
<% end %>
+ <%= f.field_container :checkout_zone_id, class: ['form-group'] do %>
+ <%= f.label :checkout_zone_id, Spree.t('zone') %>
+ <%= f.select :checkout_zone_id, options_from_collection_for_select(@zones, :id, :name, selected_checkout_zone(@store)), {}, { class: 'select2' } %>
+ <%= f.label :checkout_zone_id, Spree.t('i18n.checkout_zone_warning_html'), class: 'alert alert-warning col-12' %>
+ <%= f.error_message_on :checkout_zone_id %>
+ <% end %>
<%= f.field_container :default_locale, class: ['form-group'] do %>
<%= f.label :default_locale, Spree.t('i18n.language') %>
<%= f.select :default_locale, options_from_collection_for_select(all_locales_options, :last, :first, @store.default_locale), {}, { class: 'select2' } %>
diff --git a/backend/spec/helpers/spree/admin/stores_helper_spec.rb b/backend/spec/helpers/spree/admin/stores_helper_spec.rb
new file mode 100644
index 0000000000..cc3854ca4e
--- /dev/null
+++ b/backend/spec/helpers/spree/admin/stores_helper_spec.rb
@@ -0,0 +1,46 @@
+require 'spec_helper'
+
+describe Spree::Admin::StoresHelper, type: :helper do
+
+ describe '#selected_checkout_zone' do
+ let!(:store) { create(:store) }
+ let!(:country) { create(:country) }
+ let!(:country_zone) { create(:zone, name: 'CountryZone') }
+
+ context 'with set preference checkout_zone in spree config file' do
+ before do
+ country_zone.members.create(zoneable: country)
+ Spree::Config[:checkout_zone] = country_zone.name
+ end
+
+ it 'return countries' do
+ expect(selected_checkout_zone(store)).to eq Spree::Zone.find_by(name: Spree::Config[:checkout_zone]).id
+ end
+ end
+
+ context 'with checkout_zone_id set on store' do
+ before do
+ Spree::Config.preference_default(:checkout_zone)
+ country_zone.members.create(zoneable: country)
+ store.update(checkout_zone_id: country_zone.id)
+ end
+
+ it 'return countries' do
+ expect(selected_checkout_zone(store)).to eq country_zone.id
+ end
+ end
+
+ context 'return No Limits zone' do
+ before do
+ Spree::Config.preference_default(:checkout_zone)
+ store.update(checkout_zone_id: nil)
+ country_zone.update(name: 'No Limits')
+ country_zone.members.create(zoneable: country)
+ end
+
+ it 'return countries' do
+ expect(selected_checkout_zone(store)).to eq country_zone.id
+ end
+ end
+ end
+end
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index f6dc747c0b..6069d9407a 100644
--- a/core/config/locales/en.yml
+++ b/core/config/locales/en.yml
@@ -958,6 +958,8 @@ en:
supported_locales: Supported Locales
this_file_language: English (US)
translations: Translations
+ checkout_zone_warning_html: "Selecting a Zone will limit customers on Checkout to Addresses in Countries in that Zone. <br />
+ Please see documentation: <a href='https://guides.spreecommerce.org/user/configuration/configuring_geography.html#zones' target='_blank' class='alert-link'>https://guides.spreecommerce.org/user/configuration/configuring_geography.html#zones</a>"
icon: Icon
image: Image
images: Images
diff --git a/core/lib/spree/permitted_attributes.rb b/core/lib/spree/permitted_attributes.rb
index 5b114530b7..61b2fb87ae 100644
--- a/core/lib/spree/permitted_attributes.rb
+++ b/core/lib/spree/permitted_attributes.rb
@@ -101,7 +101,7 @@ module Spree
:customer_support_email, :facebook, :twitter, :instagram,
:description, :address, :contact_email, :contact_phone,
:default_locale, :default_country_id, :supported_currencies,
- :new_order_notifications_email, :mailer_logo]
+ :new_order_notifications_email, :mailer_logo, :checkout_zone_id]
@@store_credit_attributes = %i[amount currency category_id memo]