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.

Admin: Open-source new Store management (#12314)

Damian Legawiec730e52d71c86
diff --git a/admin/app/controllers/spree/admin/storefront_controller.rb b/admin/app/controllers/spree/admin/storefront_controller.rb
new file mode 100644
index 0000000000..72e9752d53
--- /dev/null
+++ b/admin/app/controllers/spree/admin/storefront_controller.rb
@@ -0,0 +1,37 @@
+module Spree
+ module Admin
+ class StorefrontController < BaseController
+ def edit
+ @store = current_store
+ end
+
+ def update
+ @store = current_store
+ if @store.update(store_params)
+ flash[:success] = flash_message_for(@store, :successfully_updated)
+ else
+ flash[:error] = @store.errors.full_messages.to_sentence
+ end
+
+ redirect_to spree.edit_admin_storefront_path
+ end
+
+ private
+
+ def store_params
+ params.require(:store).permit(
+ :preferred_index_in_search_engines,
+ :preferred_password_protected, :social_image, :favicon_image, :name,
+ :meta_description, :meta_title, :meta_keywords, :seo_robots,
+ :facebook, :twitter, :instagram, :linkedin, :youtube, :tiktok, :pinterest,
+ :storefront_custom_code_head, :storefront_custom_code_body_start,
+ :storefront_custom_code_body_end
+ )
+ end
+
+ def model_class
+ Spree::Store
+ end
+ end
+ end
+end
diff --git a/admin/app/controllers/spree/admin/stores_controller.rb b/admin/app/controllers/spree/admin/stores_controller.rb
new file mode 100644
index 0000000000..be09c495e6
--- /dev/null
+++ b/admin/app/controllers/spree/admin/stores_controller.rb
@@ -0,0 +1,86 @@
+module Spree
+ module Admin
+ class StoresController < Spree::Admin::BaseController
+ before_action :load_store, only: [:edit, :update]
+ before_action :normalize_supported_currencies, only: [:update]
+ before_action :normalize_supported_locales, only: [:update]
+ before_action :load_all_countries, only: [:edit, :update]
+ before_action :load_all_zones, only: [:edit, :update]
+
+ def new
+ @store = Spree::Store.new(
+ default_country_iso: current_store.default_country_iso,
+ default_currency: current_store.default_currency
+ )
+ end
+
+ def create
+ @store = Spree::Store.new(permitted_store_params)
+ @store.mail_from_address = current_store.mail_from_address
+
+ if @store.save
+ flash[:success] = flash_message_for(@store, :successfully_created)
+ # redirect in view, Turbo doesn't support redirecting to a different host
+ else
+ render :new, status: :unprocessable_entity
+ end
+ end
+
+ def edit; end
+
+ def edit_emails; end
+
+ def update
+ @store.assign_attributes(permitted_store_params)
+
+ if @store.save
+ flash[:success] = flash_message_for(@store, :successfully_updated)
+ else
+ flash[:error] = "#{Spree.t('store_errors.unable_to_update')}: #{@store.errors.full_messages.join(', ')}"
+ end
+
+ if @store.saved_changes? && permitted_store_params[:code].present?
+ redirect_to spree.admin_custom_domains_url(host: @store.url), allow_other_host: true
+ else
+ respond_to do |format|
+ format.turbo_stream
+ format.html { redirect_to spree.edit_admin_store_path(section: params[:section]) }
+ end
+ end
+ end
+
+ protected
+
+ def permitted_store_params
+ params.require(:store).permit(permitted_store_attributes)
+ end
+
+ private
+
+ def load_store
+ @store = current_store
+ end
+
+ def load_all_countries
+ @countries = Spree::Country.pluck(:name, :id)
+ end
+
+ def load_all_zones
+ @zones = Spree::Zone.pluck(:name, :id)
+ @zones.prepend([Spree.t(:no_limits_zone), ' '])
+ end
+
+ def normalize_supported_currencies
+ if params.dig(:store, :supported_currencies)&.is_a?(Array)
+ params[:store][:supported_currencies] = params[:store][:supported_currencies].compact.uniq.reject(&:blank?).join(',')
+ end
+ end
+
+ def normalize_supported_locales
+ if params.dig(:store, :supported_locales)&.is_a?(Array)
+ params[:store][:supported_locales] = params[:store][:supported_locales].compact.uniq.reject(&:blank?).join(',')
+ end
+ end
+ end
+ end
+end
diff --git a/admin/app/views/spree/admin/storefront/edit.html.erb b/admin/app/views/spree/admin/storefront/edit.html.erb
new file mode 100644
index 0000000000..f3858bd967
--- /dev/null
+++ b/admin/app/views/spree/admin/storefront/edit.html.erb
@@ -0,0 +1,142 @@
+<% content_for :page_title do %>
+ <%= Spree.t(:settings) %>
+<% end %>
+
+<%= content_for(:page_actions) do %>
+ <%= link_to_edit_translations(@store, classes: 'btn btn-light') %>
+<% end %>
+
+<%= render partial: 'spree/admin/shared/error_messages', locals: { target: @store }, class: 'mb-5 pb-5' %>
+
+<%= form_for @store, url: spree.admin_storefront_path, method: :patch do |f| %>
+ <div class="row">
+ <div class="col-lg-6">
+ <div class="card mb-4">
+ <div class="card-header">
+ <h5 class="card-title">
+ <%= Spree.t(:seo) %>
+ </h5>
+ </div>
+ <div class="card-body">
+ <div class="form-group">
+ <%= f.label :name, Spree.t(:site_name) %>
+ <%= f.text_field :name, class: 'form-control' %>
+ </div>
+ <div class="form-group">
+ <%= f.label :meta_description, 'Search description' %>
+ <%= f.text_area :meta_description, class: 'form-control', style: 'min-height: 5em', data: { controller: 'textarea-autogrow'} %>
+ </div>
+ <div class="custom-control custom-checkbox mb-2">
+ <%= f.check_box :preferred_index_in_search_engines, class: 'custom-control-input' %>
+ <%= f.label :preferred_index_in_search_engines, Spree.t(:index_in_search_engines), class: 'custom-control-label' %>
+ </div>
+ <p class="text-muted small pl-4 mb-0">
+ Checking this box will allow your site to appear in search engines.
+ </p>
+ </div>
+ </div>
+
+ <div class="card mb-4">
+ <div class="card-header">
+ <h5 class="card-title">
+ <%= Spree.t(:security) %>
+ </h5>
+ </div>
+ <div class="card-body">
+ <div class="custom-control custom-checkbox mb-2">
+ <%= f.check_box :preferred_password_protected, class: 'custom-control-input' %>
+ <%= f.label :preferred_password_protected, Spree.t(:password_protected), class: 'custom-control-label' %>
+ </div>
+ <p class="text-muted small pl-4">
+ Checking this box will put your storefront behind a password.
+ </p>
+
+ <% if @store.prefers_password_protected? %>
+ <div class="form-group">
+ <%= f.label :storefront_password, Spree.t(:password) %>
+ <%= f.text_field :storefront_password, class: 'form-control' %>
+ </div>
+ <% end %>
+ </div>
+ </div>
+
+ <div class="card mb-4">
+ <div class="card-header">
+ <h5 class="card-title">
+ <%= Spree.t(:custom_code) %>
+ </h5>
+ </div>
+ <div class="card-body">
+ <div class="form-group">
+ <%= f.label :storefront_custom_code_head, Spree.t(:head_tag) %>
+ <%= f.text_area :storefront_custom_code_head, class: 'form-control', rows: 4, data: { controller: 'textarea-autogrow'} %>
+ </div>
+
+ <div class="form-group">
+ <%= f.label :storefront_custom_code_body_start, Spree.t(:body_tag_start) %>
+ <%= f.text_area :storefront_custom_code_body_start, class: 'form-control', rows: 4, data: { controller: 'textarea-autogrow'} %>
+ </div>
+
+ <div class="form-group">
+ <%= f.label :storefront_custom_code_body_end, Spree.t(:body_tag_end) %>
+ <%= f.text_area :storefront_custom_code_body_end, class: 'form-control', rows: 4, data: { controller: 'textarea-autogrow'} %>
+ </div>
+ </div>
+ </div>
+ </div>
+
+ <div class="col-lg-6">
+ <div class="card mb-4">
+ <div class="card-header">
+ <h5 class="card-title">
+ <%= Spree.t(:images) %>
+ </h5>
+ </div>
+ <div class="card-body">
+ <div class="form-group">
+ <%= f.label :favicon_image do %>
+ Favicon image
+ <%= help_bubble("A favicon is a small image that appears in the tab of your web browser when you visit a website. It is usually a tiny square or rectangle with a unique design or logo that represents the website") %>
+ <% end %>
+ <%= render 'active_storage/upload_form', form: f, field_name: :favicon_image, width: 120, height: 120, crop: true %>
+ </div>
+
+ <div class="form-group mb-0">
+ <%= f.label :social_image do %>
+ Preview image
+ <%= help_bubble("This image will be used when your store is shared on social media or in search engines.") %>
+ <% end %>
+ <%= render 'active_storage/upload_form', form: f, field_name: :social_image, width: 1200, height: 630, crop: true %>
+ </div>
+ </div>
+ </div>
+
+ <div class="card mb-4">
+ <div class="card-header">
+ <h5 class="card-title">
+ <%= Spree.t(:social_media) %>
+ </h5>
+ </div>
+ <div class="card-body">
+ <div class="alert alert-info">
+ Social links will be displayed in the footer of your store
+ </div>
+ <% Spree::Store::SUPPORTED_SOCIAL_NETWORKS.each do |social| %>
+ <div class="form-group">
+ <%= f.label :"store_#{social}" do %>
+ <%= icon social %>
+ <%= social.capitalize %>
+ <% end %>
+ <%= f.text_field social.to_sym,
+ class: 'form-control',
+ placeholder: Spree::Store::SOCIAL_NETWORKS_CONFIG[social.to_sym][:input_placeholder]
+ %>
+ </div>
+ <% end %>
+ </div>
+ </div>
+ </div>
+ </div>
+
+ <%= render 'spree/admin/stores/form/buttons' %>
+<% end %>
diff --git a/admin/app/views/spree/admin/stores/create.turbo_stream.erb b/admin/app/views/spree/admin/stores/create.turbo_stream.erb
new file mode 100644
index 0000000000..7102935f9b
--- /dev/null
+++ b/admin/app/views/spree/admin/stores/create.turbo_stream.erb
@@ -0,0 +1,5 @@
+<%= turbo_stream.replace "modal_scripts" do %>
+ <script>
+ window.location.href = "<%= spree.admin_dashboard_url(host: @store.url) %>";
+ </script>
+<% end %>
\ No newline at end of file
diff --git a/admin/app/views/spree/admin/stores/edit.html.erb b/admin/app/views/spree/admin/stores/edit.html.erb
new file mode 100644
index 0000000000..ebb9e662bb
--- /dev/null
+++ b/admin/app/views/spree/admin/stores/edit.html.erb
@@ -0,0 +1,13 @@
+<% content_for :head do %>
+ <script type="text/javascript" src="https://cdn.goentri.com/entri.js"></script>
+<% end if entri_enabled? %>
+
+<%= render partial: 'spree/admin/shared/error_messages', locals: { target: @store }, class: 'mb-5 pb-5' %>
+
+<%= form_for current_store, url: spree.admin_store_path(section: params[:section]), method: :patch do |f| %>
+ <%= render partial: 'spree/admin/stores/form/basic', locals: { f: f } if params[:section].blank? || params[:section] == 'general-settings' %>
+ <%= render partial: 'spree/admin/stores/form/emails', locals: { f: f } if params[:section] == 'emails' %>
+ <%= render partial: 'spree/admin/stores/form/checkout', locals: { f: f } if params[:section] == 'checkout' %>
+ <%= render partial: 'spree/admin/stores/form/policies', locals: { f: f } if params[:section] == 'policies' %>
+ <%= render 'spree/admin/stores/form/buttons' %>
+<% end %>
diff --git a/admin/app/views/spree/admin/stores/edit_emails.html.erb b/admin/app/views/spree/admin/stores/edit_emails.html.erb
new file mode 100644
index 0000000000..5922e23b03
--- /dev/null
+++ b/admin/app/views/spree/admin/stores/edit_emails.html.erb
@@ -0,0 +1,11 @@
+<%= turbo_frame_tag :set_customer_support_email, class: 'w-100' do %>
+ <%= form_for current_store, url: spree.admin_store_path, as: :store do |f| %>
+ <div class="form-group">
+ <%= f.label :customer_support_email, 'Customer support email' %>
+ <%= f.email_field :customer_support_email, class: 'form-control', required: true %>
+ </div>
+ <div class="d-flex justify-content-between">
+ <%= turbo_save_button_tag %>
+ </div>
+ <% end %>
+<% end %>
\ No newline at end of file
diff --git a/admin/app/views/spree/admin/stores/form/_basic.html.erb b/admin/app/views/spree/admin/stores/form/_basic.html.erb
new file mode 100644
index 0000000000..6415e9479a
--- /dev/null
+++ b/admin/app/views/spree/admin/stores/form/_basic.html.erb
@@ -0,0 +1,114 @@
+<%= content_for(:page_title) do %>
+ <%= Spree.t(:store_details) %>
+<% end %>
+
+<%= content_for(:page_actions) do %>
+ <%= link_to_edit_translations(@store, classes: 'btn btn-light') %>
+<% end %>
+
+<div class="row">
+ <div class="col-lg-6">
+ <div class="card mb-4">
+ <div class="card-header">
+ <h5 class="card-title"><%= Spree.t(:name) %> & <%= Spree.t(:logo) %></h5>
+ </div>
+ <div class="card-body">
+ <p class="text-muted">
+ This is the name of your store. It will be displayed in the header and in the footer.
+ </p>
+ <div class="form-group">
+ <%= f.label :name, Spree.t(:name) %>
+ <%= f.text_field :name, class: 'form-control', required: true %>
+ <%= f.error_message_on :name %>
+ </div>
+
+ <div class="form-group mb-0">
+ <%= f.label :logo do %>
+ <%= Spree.t(:logo) %>
+ <%= help_bubble("Used in the admin dashboard") %>
+ <% end %>
+ <%= render 'active_storage/upload_form', form: f, field_name: :logo, width: 240, height: 240, crop: true %>
+ </div>
+ </div>
+ </div>
+
+ <div class="card mb-4">
+ <div class="card-header">
+ <h5 class="card-title"><%= Spree.t(:standards_and_formats) %></h5>
+ </div>
+ <div class="card-body">
+ <p class="text-muted">
+ Set the timezone, unit system and weight unit for your store.
+ </p>
+ <div class="form-group mb-3">
+ <%= label_tag :preferred_timezone, raw(Spree.t(:timezone) + required_span_tag) %>
+ <%= time_zone_select :store, :preferred_timezone, nil, {}, { data: { controller: 'autocomplete-select' } } %>
+ </div>
+ <div data-controller="unit-system">
+ <div class="form-group mb-3">
+ <%= label_tag :preferred_unit_system, raw(Spree.t(:unit_system) + required_span_tag) %>
+ <%= select_tag 'store[preferred_unit_system]', options_for_select(unit_systems, @store.preferred_unit_system), {class: 'custom-select', data: {unit_system_target: "body", action: "change->unit-system#unitSystemHandler"}} %>
+ </div>
+ <div class="form-group">
+ <%= label_tag :preferred_weight_unit, raw(Spree.t(:weight_unit) + required_span_tag) %>
+ <%= select_tag 'store[preferred_weight_unit]', options_for_select(weight_units(@store), @store.preferred_weight_unit), {class: 'custom-select', data: {unit_system_target: "weightUnit"}} %>
+ </div>
+ </div>
+ </div>
+ </div>
+ </div>
+
+ <div class="col-lg-6">
+ <div class="card mb-4">
+ <div class="card-header">
+ <h5 class="card-title"><%= Spree.t(:currencies) %> & <%= Spree.t(:countries) %></h5>
+ </div>
+ <div class="card-body">
+ <p class="text-muted">
+ This determines which vendors you can connect with and what customers see on your store.
+ </p>
+ <div class="form-group">
+ <%= f.label :default_currency, Spree.t(:currency) %>
+ <%= f.currency_select :default_currency, preferred_currencies, {}, data: { controller: 'autocomplete-select' } %>
+ <%= f.error_message_on :default_currency %>
+ </div>
+ <div class="form-group">
+ <%= f.label :supported_currencies, Spree.t(:supported_currencies) %>
+ <%= f.select :supported_currencies,
+ currency_options(@store.supported_currencies&.split(',')),
+ { prompt: false },
+ { multiple: true, data: { controller: 'autocomplete-select' } } %>
+ <%= f.error_message_on :supported_currencies %>
+ </div>
+
+ <div class="form-group">
+ <%= f.label :default_country_iso, Spree.t('i18n.country') %>
+ <%= f.country_select :default_country_iso, {}, { data: { controller: 'autocomplete-select' } } %>
+ <%= f.error_message_on :default_country_iso %>
+ </div>
+ <div class="form-group">
+ <%= f.label :checkout_zone_id, Spree.t(:shipping_zone) %>
+ <%= f.select :checkout_zone_id, options_for_select(@zones, @store.checkout_zone_id), { }, { data: { controller: 'autocomplete-select' } } %>
+ <small class="form-text text-muted mt-2">
+ Zone will limit to which Countries or States products are shipped.
+ <%= link_to Spree.t(:manage_zones), spree.admin_zones_path %>
+ </small>
+ <%= f.error_message_on :checkout_zone_id %>
+ </div>
+
+ <div class="form-group">
+ <%= f.label :default_locale %>
+ <%= f.select :default_locale, options_from_collection_for_select(all_locales_options, :last, :first, @store.default_locale || I18n.locale), {}, { data: { controller: 'autocomplete-select' } } %>
+ <%= f.error_message_on :default_locale %>
+ </div>
+ <div class="form-group">
+ <%= f.label :supported_locales, Spree.t(:supported_locales) %>
+ <%= f.select :supported_locales, options_from_collection_for_select(all_locales_options, :last, :first, @store.supported_locales&.split(',')), {}, { multiple: true, data: { controller: 'autocomplete-select' } } %>
+ <%= f.error_message_on :supported_locales %>
+ </div>
+ </div>
+ </div>
+ </div>
+
+ <%= render 'spree/admin/stores/form/custom', f: f %>
+</div>
diff --git a/admin/app/views/spree/admin/stores/form/_buttons.html.erb b/admin/app/views/spree/admin/stores/form/_buttons.html.erb
new file mode 100644
index 0000000000..9afe007d54
--- /dev/null
+++ b/admin/app/views/spree/admin/stores/form/_buttons.html.erb
@@ -0,0 +1,3 @@
+<div class="form-actions">
+ <%= turbo_save_button_tag Spree.t('actions.update') %>
+</div>
diff --git a/admin/app/views/spree/admin/stores/form/_checkout.html.erb b/admin/app/views/spree/admin/stores/form/_checkout.html.erb
new file mode 100644
index 0000000000..cafee994a6
--- /dev/null
+++ b/admin/app/views/spree/admin/stores/form/_checkout.html.erb
@@ -0,0 +1,28 @@
+<%= content_for(:page_title) do %>
+ Checkout settings
+<% end %>
+
+<%= content_for :page_alerts do %>
+ <div class="alert alert-info mb-3">
+ <p>
+ Checkout footer links are added automatically based on the <%= link_to 'Policies', spree.edit_admin_store_path + '?section=policies', class: 'alert-link' %> content.
+ </p>
+ </div>
+<% end %>
+
+<div class="card-lg">
+ <h5 class="mb-3"><%= Spree.t(:checkout_message) %></h5>
+
+ <div class="row">
+ <div class="col-lg-4">
+ <p class="text-muted">
+ Visible to your customers on the checkout page in the right sidebar.
+ </p>
+ </div>
+ <div class="col-lg-7 offset-lg-1">
+ <div class="trix-container">
+ <%= f.rich_text_area :checkout_message, class: 'overflow-auto', style: 'height: 300px' %>
+ </div>
+ </div>
+ </div>
+</div>
diff --git a/admin/app/views/spree/admin/stores/form/_custom.html.erb b/admin/app/views/spree/admin/stores/form/_custom.html.erb
new file mode 100644
index 0000000000..70b7404d3d
--- /dev/null
+++ b/admin/app/views/spree/admin/stores/form/_custom.html.erb
@@ -0,0 +1,79 @@
+<div class="col-lg-6">
+ <div class="card mb-4">
+ <div class="card-header">
+ <h5 class="card-title">Vendor Commission rate</h5>
+ </div>
+ <div class="card-body">
+ <p class="text-muted">
+ This is the default commission rate, you can override this value for each Partner, Category x Vendor and Vendor product.
+ </p>
+ <div class="alert alert-info">
+ <p>
+ This change will only apply to newly invited
+ <%= link_to 'Partners', spree.admin_vendors_path, class: 'alert-link' %>.
+ It will not change the commission rate for existing Partners.
+ </p>
+ <p>
+ Changing this option will also change payout schedules for your existing Vendors with the
+ payout schedule set to <strong>Use marketplace default interval</strong>.
+ </p>
+ It will not change the commission rate for existing Partners.
+ </div>
+ <div class="form-group">
+ <%= f.label :preferred_platform_fee, 'Commission rate' %>
+ <div class="form-control d-flex align-items-center py-0 focus-shadow-blue">
+ <%= f.number_field :preferred_platform_fee, step: 0.1, min: 0, class: 'form-control-plaintext pl-0' %>
+ <span>%</span>
+ </div>
+ <%= f.error_message_on :preferred_platform_fee %>
+ </div>
+ </div>
+ </div>
+</div>
+
+<div class="col-lg-6">
+ <div class="card mb-4">
+ <div class="card-header">
+ <h5 class="card-title">Vendor Payouts</h5>
+ </div>
+ <div class="card-body">
+ <p class="text-muted">
+ This controls how often your Vendors will be able to request a payout in Stripe dashboard.
+ </p>
+ <div class="alert alert-info">
+ <p>
+ Changing this option will also change payout schedules for your existing Vendors with the
+ payout schedule set to <strong>Use marketplace default interval</strong>.
+ </p>
+ </div>
+ <div class="form-group">
+ <%= f.label :preferred_vendor_payouts_schedule_interval, Spree.t(:preferred_vendor_payouts_schedule_interval) %>
+ <%= f.select :preferred_vendor_payouts_schedule_interval,
+ options_for_select(stripe_store_payouts_schedule_interval_options, @store.preferred_vendor_payouts_schedule_interval),
+ {},
+ { class: 'custom-select' } %>
+ <%= f.error_message_on :preferred_vendor_payouts_schedule_interval %>
+ </div>
+ </div>
+ </div>
+</div>
+
+<% if Flipper.enabled?(:jwt_auth, current_store) %>
+ <div class="col-lg-6">
+ <div class="card">
+ <div class="card-header">
+ <h5 class="card-title">JWT Authentication</h5>
+ </div>
+ <div class="card-body">
+ <div class="form-group">
+ <%= f.label :jwks_endpoint, Spree.t(:jwks_endpoint) %>
+ <%= f.text_field :jwks_endpoint, class: 'form-control', type: :url %>
+ <%= f.error_message_on :jwks_endpoint %>
+ <p class="form-text text-muted">
+ Make sure that your JWK keys have the <code>kid</code> property set and payload includes <code>email</code>, <code>iat</code>, <code>exp</code>.
+ </p>
+ </div>
+ </div>
+ </div>
+ </div>
+<% end %>
\ No newline at end of file
diff --git a/admin/app/views/spree/admin/stores/form/_emails.html.erb b/admin/app/views/spree/admin/stores/form/_emails.html.erb
new file mode 100644
index 0000000000..f744d84e0c
--- /dev/null
+++ b/admin/app/views/spree/admin/stores/form/_emails.html.erb
@@ -0,0 +1,59 @@
+<%= content_for(:page_title) do %>
+ Email settings
+<% end %>
+
+<div class="row">
+ <div class="col-lg-6">
+ <div class="card mb-4">
+ <div class="card-header">
+ <h5 class="card-title">Logo for emails</h5>
+ </div>
+ <div class="card-body">
+ <p class="text-muted">This logo is included in all email notifications such as order confirmation</p>
+ <%= render 'active_storage/upload_form', form: f, field_name: :mailer_logo, width: 204, height: 104 %>
+ <p class="text-muted mb-0">
+ We recommend images with a transparent background.
+ <br />
+ Only JPEG and PNG formats are supported.
+ </p>
+ </div>
+ </div>
+ </div>
+
+ <div class="col-lg-6">
+ <div class="card mb-4">
+ <div class="card-header">
+ <h5 class="card-title">Email addresses</h5>
+ </div>
+ <div class="card-body">
+ <p class="text-muted">
+ Please provide the email address that will be used as the sender of all emails sent from your store.
+ </p>
+
+ <div class="form-group">
+ <%= f.label :mail_from_address, raw(Spree.t(:mail_from_address) + required_span_tag) %>
+ <%= f.text_field :mail_from_address, class: 'form-control', required: true %>
+ <%= f.error_message_on :mail_from_address %>
+ </div>
+
+ <div class="form-group">
+ <%= f.label :customer_support_email, Spree.t(:customer_support_email) %>
+ <%= f.text_field :customer_support_email, class: 'form-control' %>
+ <%= f.error_message_on :customer_support_email %>
+ <small class="form-text text-muted mt-2">
+ <%= Spree.t('admin.store_form.customer_support_email_help') %>
+ </small>
+ </div>
+
+ <div class="form-group">
+ <%= f.label :new_order_notifications_email, Spree.t(:new_order_notifications_email) %>
+ <%= f.text_field :new_order_notifications_email, class: 'form-control' %>
+ <%= f.error_message_on :new_order_notifications_email %>
+ <small class="form-text text-muted mt-2">
+ <%= Spree.t('admin.store_form.new_order_notifications_email_help') %>
+ </small>
+ </div>
+ </div>
+ </div>
+ </div>
+</div>
diff --git a/admin/app/views/spree/admin/stores/form/_policies.html.erb b/admin/app/views/spree/admin/stores/form/_policies.html.erb
new file mode 100644
index 0000000000..7a30b2204c
--- /dev/null
+++ b/admin/app/views/spree/admin/stores/form/_policies.html.erb
@@ -0,0 +1,85 @@
+<%= content_for(:page_title) do %>
+ <%= Spree.t(:policies) %>
+<% end %>
+
+<%= content_for :page_alerts do %>
+ <div class="alert alert-info mb-3">
+ Policies will be automatically added to footer of your store and will be available to customers during checkout.
+ </div>
+<% end %>
+
+<div class="card-lg">
+ <h5 class="mb-3"><%= Spree.t(:terms_of_service) %></h5>
+ <div class="row">
+ <div class="col-lg-4">
+ <p class="text-muted">
+ Customers will need to accept these terms of service during signup.
+ </p>
+ </div>
+ <div class="col-lg-7 offset-lg-1">
+ <div class="trix-container">
+ <%= f.rich_text_area :customer_terms_of_service, class: 'overflow-auto', style: 'height: 300px' %>
+ </div>
+ </div>
+ </div>
+ <hr class="my-5" />
+ <h5 class="mb-3"><%= Spree.t(:privacy_policy) %></h5>
+ <div class="row">
+ <div class="col-lg-4">
+ <p class="text-muted">
+ Customers will need to accept these privacy policy during signup.
+ </p>
+ </div>
+ <div class="col-lg-7 offset-lg-1">
+ <div class="trix-container">
+ <%= f.rich_text_area :customer_privacy_policy, class: 'overflow-auto', style: 'height: 300px' %>
+ </div>
+ </div>
+ </div>
+
+ <hr class="my-5" />
+ <h5 class="mb-3"><%= Spree.t(:returns_policy) %></h5>
+ <div class="row">
+ <div class="col-lg-4">
+ <p class="text-muted">
+ Please provide your customers with a clear and transparent returns policy.
+ </p>
+ </div>
+ <div class="col-lg-7 offset-lg-1">
+ <div class="trix-container">
+ <%= f.rich_text_area :customer_returns_policy, class: 'overflow-auto', style: 'height: 300px' %>
+ </div>
+ </div>
+ </div>
+ <hr class="my-5" />
+ <h5 class="mb-3"><%= Spree.t(:shipping_policy) %></h5>
+ <div class="row">
+ <div class="col-lg-4">
+ <p class="text-muted">
+ Please provide your customers with a clear and transparent shipping policy.
+ </p>
+ </div>
+ <div class="col-lg-7 offset-lg-1">
+ <div class="trix-container">
+ <%= f.rich_text_area :customer_shipping_policy, class: 'overflow-auto', style: 'height: 300px' %>
+ </div>
+ </div>
+ </div>
+
+ <% if current_store.respond_to?(:partners_terms_of_service) %>
+ <hr class="my-5">
+ <h5 class="mb-3">Vendors <%= Spree.t(:terms_of_service) %></h5>
+ <div class="row" id="partners-terms">
+ <div class="col-lg-4">
+ <p class="text-muted">
+ Vendors you invite will need to accept these terms of service during their onboarding process.
+ </p>
+ </div>
+ <div class="col-lg-7 offset-lg-1">
+ <div class="trix-container">
+ <%= f.rich_text_area :partners_terms_of_service, class: 'overflow-auto', style: 'height: 300px' %>
+ </div>
+ </div>
+ </div>
+ <% end %>
+</div>
diff --git a/admin/app/views/spree/admin/stores/new.html.erb b/admin/app/views/spree/admin/stores/new.html.erb
new file mode 100644
index 0000000000..f930554338
--- /dev/null
+++ b/admin/app/views/spree/admin/stores/new.html.erb
@@ -0,0 +1,135 @@
+<div class="modal fade" id="new-store-modal" tabindex="-1" aria-labelledby="newStoreModalLabel" aria-hidden="true">
+ <div class="modal-dialog modal-dialog-centered">
+ <%= turbo_frame_tag :dialog_modal_lg do %>
+ <%= form_for @store, url: spree.admin_stores_path, data: { turbo_frame: '_top', controller: 'store-form enable-button' } do |f| %>
+ <div class="modal-content">
+ <div class="modal-header">
+ <h5 class="modal-title" id="newStoreModalLabel"><%= Spree.t(:new_store) %></h5>
+ <button type="button" class="btn-close" data-dismiss="modal" aria-label="<%= Spree.t(:close) %>"></button>
+ </div>
+ <div class="modal-body">
+ <%= render partial: 'spree/admin/shared/error_messages', locals: { target: @store } %>
+ <div class="row">
+ <div class="col-lg-6">
+ <%= render partial: 'spree/admin/shared/error_messages', locals: { target: @store } %>
+ <div class="form-group">
+ <%= f.label :name %>
+ <%= f.text_field :name, class: 'form-control',
+ placeholder: Spree.t(:store_name),
+ required: true,
+ data: {
+ enable_button_target: 'input',
+ } %>
+ </div>
+
+ <div class="form-group">
+ <%= f.label :default_country_iso, Spree.t(:country) %>
+ <%= f.collection_select :default_country_iso, Spree::Country.order(:name), :iso, :name, { }, { data: { controller: 'autocomplete-select', enable_button_target: 'input', action: 'store-form#updateCurrency' } } %>
+ </div>
+
+ <div class="form-group">
+ <%= f.label :default_currency, Spree.t(:currency) %>
+ <%= f.currency_select :default_currency, preferred_currencies, {}, { data: { controller: 'autocomplete-select', enable_button_target: 'input', store_form_target: 'currency' } } %>
+ </div>
+
+ <% if available_stores.any? %>
+ <div class="form-group">
+ <%= f.label :import_products_from_store_id, Spree.t(:import_products_from) %>
+ <%= f.select :import_products_from_store_id,
+ options_for_select(available_stores.pluck(:name, :id)),
+ { include_blank: Spree.t(:do_not_import) },
+ {
+ data: { controller: 'autocomplete-select' }
+ } %>
+ </div>
+
+ <div class="form-group">
+ <%= f.label :import_payment_methods_from_store_id, Spree.t(:import_payment_methods_from) %>
+ <%= f.select :import_payment_methods_from_store_id,
+ options_for_select(available_stores.pluck(:name, :id)),
+ { include_blank: Spree.t(:do_not_import) },
+ {
+ data: { controller: 'autocomplete-select' }
+ } %>
+ </div>
+ <% end %>
+ </div>
+ <div class="col-lg-6">
+ <h5>What will be shared with this new store:</h5>
+ <ul class="list-unstyled">
+ <li>
+ <%= icon('check', class: 'text-success') %>
+ <%= Spree.t(:products) %>, <%= Spree.t(:stock_locations) %> & <%= Spree.t(:inventory) %><br />
+ <em class="ml-4 text-muted font-size-sm">you can select which ones will be available for this store</em>
+ </li>
+ <li>
+ <%= icon('check', class: 'text-success') %>
+ <%= Spree.t(:customers) %>
+ </li>
+ <li>
+ <%= icon('check', class: 'text-success') %>
+ Shipping methods, rates and markets
+ </li>
+ <li>
+ <%= icon('check', class: 'text-success') %>
+ <%= Spree.t(:payment_methods) %><br />
+ <em class="ml-4 text-muted font-size-sm">you can select which ones will be available for this store</em>
+ </li>
+ <li>
+ <%= icon('check', class: 'text-success') %>
+ Admin user privileges
+ </li>
+ </ul>
+
+ <h5>What will not be shared with this new store:</h5>
+ <ul class="list-unstyled">
+ <li>
+ <%= icon('x', class: 'text-danger') %>
+ <%= Spree.t(:orders) %>
+ </li>
+ <li>
+ <%= icon('x', class: 'text-danger') %>
+ <%= Spree.t(:shipments) %>
+ </li>
+ <li>
+ <%= icon('x', class: 'text-danger') %>
+ <%= Spree.t(:payments) %>
+ </li>
+ <li>
+ <%= icon('x', class: 'text-danger') %>
+ <%= Spree.t(:refunds) %>
+ </li>
+ <li>
+ <%= icon('x', class: 'text-danger') %>
+ <%= Spree.t(:store_credits) %>
+ </li>
+ <li>
+ <%= icon('x', class: 'text-danger') %>
+ <%= Spree.t(:gift_cards) %>
+ </li>
+ <li>
+ <%= icon('x', class: 'text-danger') %>
+ <%= Spree.t(:blogs_posts) %>
+ </li>
+ <li>
+ <%= icon('x', class: 'text-danger') %>
+ <%= Spree.t(:themes) %> & <%= Spree.t(:pages) %>
+ </li>
+ <li>
+ <%= icon('x', class: 'text-danger') %>
+ <%= Spree.t(:integrations) %> eg. Google Analytics, Meta Pixel, etc.
+ </li>
+ </ul>
+ </div>
+ </div>
+ </div>
+ <div class="modal-footer justify-content-between">
+ <button type="button" class="btn btn-light" data-dismiss="modal"><%= Spree.t('actions.discard') %></button>
+ <%= turbo_save_button_tag Spree.t('actions.create') %>
+ </div>
+ </div>
+ </div>
+ <% end %>
+ <% end %>
+ </div>
+</div>
\ No newline at end of file
diff --git a/admin/app/views/spree/admin/stores/new.turbo_stream.erb b/admin/app/views/spree/admin/stores/new.turbo_stream.erb
new file mode 100644
index 0000000000..1a55f650d3
--- /dev/null
+++ b/admin/app/views/spree/admin/stores/new.turbo_stream.erb
@@ -0,0 +1 @@
+<%= render template: 'spree/admin/stores/new', formats: [:html] %>
diff --git a/admin/app/views/spree/admin/stores/update.turbo_stream.erb b/admin/app/views/spree/admin/stores/update.turbo_stream.erb
new file mode 100644
index 0000000000..126ee5e6a0
--- /dev/null
+++ b/admin/app/views/spree/admin/stores/update.turbo_stream.erb
@@ -0,0 +1,11 @@
+<% current_store.reload %>
+
+<%= turbo_stream.replace :setup_progress do %>
+ <%= render 'spree/admin/dashboard/setup_progress' %>
+<% end %>
+
+<%= turbo_stream.replace :setup_tasks do %>
+ <%= render 'spree/admin/dashboard/setup_tasks' %>
+<% end %>
+
+<%= turbo_render_alerts %>
\ No newline at end of file
diff --git a/admin/config/routes.rb b/admin/config/routes.rb
index 3cccda8de1..f59753c5e3 100644
--- a/admin/config/routes.rb
+++ b/admin/config/routes.rb
@@ -5,59 +5,56 @@ Spree::Core::Engine.add_routes do
resources :option_types, except: :show do
resources :option_values, only: [:update]
end
-
- # media library
- resources :assets, only: [:create, :edit, :update, :destroy] do
+ resources :products do
collection do
- delete :bulk_destroy
+ get :select_options, defaults: { format: :json }
+ post :search
+ get :bulk_modal
+ put :bulk_status_update
+ put :bulk_add_to_taxons
+ put :bulk_remove_from_taxons
+ put :bulk_add_tags
+ put :bulk_remove_tags
end
- end
-
- # audit log
- resources :exports, only: %i[new create show index]
-
- # profile settings
- resource :profile, controller: 'profile', only: %i[edit update]
-
- # store settings
- resource :store, only: [:edit, :update], controller: 'stores' do
member do
- get :edit_emails
+ post :clone
end
+ resources :variants, only: [:edit, :update, :destroy]
+ resources :digitals, only: [:index, :create, :destroy]
end
-
- resources :payment_methods, except: :show
- resources :shipping_methods, except: :show
- resources :shipping_categories, except: :show
- resources :store_credit_categories
- resources :tax_rates, except: :show
- resources :tax_categories, except: :show
- resources :reimbursement_types
- resources :refund_reasons, except: :show
- resources :return_authorization_reasons, except: :show
- resources :zones
- resources :stock_locations, except: :show do
- member do
- put :mark_as_default
+ # variant search
+ post 'variants/search'
+ get 'variants/search', defaults: { format: :json }
+ # stock
+ resources :stock_items, only: [:index, :update, :destroy]
+ resources :stock_transfers, except: [:edit, :update]
+ # taxonomies and taxons
+ resources :taxonomies do
+ resources :taxons do
+ member do
+ put :reposition
+ end
end
end
+ resources :taxons, except: [:show] do |_taxon|
+ resources :classifications, only: %i[index new create update destroy]
+ end
+ get '/taxons/select_options' => 'taxons#select_options', as: :taxons_select_options, defaults: { format: :json }
- # account management
- resources :roles, except: :show
-
- # developer tools
- resources :oauth_applications
- resources :webhooks_subscribers
+ # media library
+ resources :assets, only: [:create, :edit, :update, :destroy] do
+ collection do
+ delete :bulk_destroy
+ end
+ end
# orders
resources :checkouts, only: %i[index]
-
resources :orders, only: [:index, :edit, :create] do
member do
post :resend
put :cancel
end
-
resource :shipping_address, except: [:index, :show], controller: 'orders/shipping_address'
resource :billing_address, except: [:index, :show], controller: 'orders/billing_address'
resource :contact_information, only: [:edit, :update], controller: 'orders/contact_information'
@@ -70,13 +67,11 @@ Spree::Core::Engine.add_routes do
end
end
resources :line_items, except: :show
-
resources :customer_returns, only: [:index, :new, :edit, :create, :update] do
member do
put :refund
end
end
-
resources :return_authorizations do
member do
put :cancel
@@ -87,11 +82,9 @@ Spree::Core::Engine.add_routes do
put :capture
put :void
end
-
resources :refunds, only: [:new, :create, :edit, :update]
end
resources :payment_links, only: [:create], controller: 'orders/payment_links'
-
resources :reimbursements, only: [:index, :create, :show, :edit, :update] do
member do
post :perform
@@ -99,52 +92,49 @@ Spree::Core::Engine.add_routes do
end
end
- # products
- resources :products do
- collection do
- get :select_options, defaults: { format: :json }
- post :search
- get :bulk_modal
- put :bulk_status_update
- put :bulk_add_to_taxons
- put :bulk_remove_from_taxons
- put :bulk_add_tags
- put :bulk_remove_tags
- end
+ # customers
+ resources :users
- member do
- post :clone
- end
+ # translations
+ resources :translations, only: [:edit, :update], path: '/translations/:resource_type'
- resources :variants, only: [:edit, :update, :destroy]
- resources :digitals, only: [:index, :create, :destroy]
- end
+ # audit log
+ resources :exports, only: %i[new create show index]
- # stock
- resources :stock_items, only: [:index, :update, :destroy]
- resources :stock_transfers, except: [:edit, :update]
+ # profile settings
+ resource :profile, controller: 'profile', only: %i[edit update]
- # taxonomies and taxons
- resources :taxonomies do
- resources :taxons do
- member do
- put :reposition
- end
+ # store settings
+ resource :store, only: [:edit, :update], controller: 'stores' do
+ # needed for the getting started set customer support email step
+ member do
+ get :edit_emails
end
end
-
- resources :taxons, except: [:show] do |_taxon|
- resources :classifications, only: %i[index new create update destroy]
+ # setting up a new store
+ resources :stores, only: [:new, :create]
+ resources :payment_methods, except: :show
+ resources :shipping_methods, except: :show
+ resources :shipping_categories, except: :show
+ resources :store_credit_categories
+ resources :tax_rates, except: :show
+ resources :tax_categories, except: :show
+ resources :reimbursement_types
+ resources :refund_reasons, except: :show
+ resources :return_authorization_reasons, except: :show
+ resources :zones
+ resources :stock_locations, except: :show do
+ member do
+ put :mark_as_default
+ end
end
- get '/taxons/select_options' => 'taxons#select_options', as: :taxons_select_options, defaults: { format: :json }
-
- # users
- resources :users
+ # account management
+ resources :roles, except: :show
- # variants
- post 'variants/search'
- get 'variants/search', defaults: { format: :json }
+ # developer tools
+ resources :oauth_applications
+ resources :webhooks_subscribers
# errors
get '/forbidden', to: 'errors#forbidden', as: :forbidden
diff --git a/admin/spec/controllers/spree/admin/stores_controller_spec.rb b/admin/spec/controllers/spree/admin/stores_controller_spec.rb
new file mode 100644
index 0000000000..331e382953
--- /dev/null
+++ b/admin/spec/controllers/spree/admin/stores_controller_spec.rb
@@ -0,0 +1,59 @@
+require 'spec_helper'
+
+describe Spree::Admin::StoresController do
+ stub_authorization!
+ render_views
+
+ let(:store) { Spree::Store.default }
+ let!(:uk_country) { create(:country, iso: 'GB', iso3: 'GBR', name: 'United Kingdom') }
+
+ let(:image_file) { Rack::Test::UploadedFile.new(File.join(Rails.root, 'spec', 'fixtures', 'thinking-cat.jpg'), 'image/jpeg') }
+ let(:store_with_logo) { create(:store, logo: image_file) }
+
+ describe 'POST #create' do
+ subject { post :create, params: { store: store_params }, format: :turbo_stream }
+
+ let(:store_params) do
+ {
+ name: 'New UK Store',
+ default_currency: 'GBR',
+ default_country_iso: 'GB'
+ }
+ end
+
+ it 'creates a new store' do
+ expect { subject }.to change(Spree::Store, :count).by(1)
+
+ expect(flash[:success]).to match('has been successfully created')
+
+ store = Spree::Store.last
+ expect(store.name).to eq('New UK Store')
+ expect(store.default_currency).to eq('GBR')
+ expect(store.default_country).to eq(uk_country)
+ end
+ end
+
+ describe 'PUT #update' do
+ subject { patch :update, params: { id: store.id, store: store_params } }
+
+ let(:store_params) do
+ {
+ name: 'New Store Name',
+ default_currency: 'GBR',
+ default_country_iso: 'GB'
+ }
+ end
+
+ before do
+ store.update!(checkout_zone: nil)
+ end
+
+ it 'updates the store data' do
+ subject
+
+ expect(store.reload.name).to eq('New Store Name')
+ expect(store.default_currency).to eq('GBR')
+ expect(store.default_country).to eq(uk_country)
+ end
+ end
+end
diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rb
index 7d450ae3ec..ade49621f0 100644
--- a/core/app/models/spree/store.rb
+++ b/core/app/models/spree/store.rb
@@ -1,5 +1,9 @@
module Spree
class Store < Spree.base_class
+ RESERVED_CODES = %w(
+ admin default app api www cdn files assets checkout account auth login user
+ )
+
include FriendlyId
include Spree::TranslatableResource
include Spree::Metadata
@@ -118,7 +122,7 @@ module Spree
end
validates :preferred_digital_asset_authorized_clicks, numericality: { only_integer: true, greater_than: 0 }
validates :preferred_digital_asset_authorized_days, numericality: { only_integer: true, greater_than: 0 }
- validates :code, uniqueness: { case_sensitive: false, conditions: -> { with_deleted } }
+ validates :code, uniqueness: { case_sensitive: false, conditions: -> { with_deleted } }, exclusion: RESERVED_CODES
validates :mail_from_address, email: { allow_blank: false }
# FIXME: we should remove this condition in v5
if !ENV['SPREE_DISABLE_DB_CONNECTION'] &&
@@ -140,6 +144,7 @@ module Spree
#
# Callbacks
before_validation :ensure_default_country
+ before_validation :set_code, on: :create
before_save :ensure_default_exists_and_is_unique
before_save :ensure_supported_currencies, :ensure_supported_locales
after_create :ensure_default_taxonomies_are_created
@@ -437,5 +442,20 @@ module Spree
def handle_code_changes
# implement your custom logic here
end
+
+ # This FriendlyId method is overwitten to keep our logic for generating code
+ # there is no option for own format
+ def set_code
+ self.code = if code.present?
+ code.parameterize.strip
+ elsif name.present?
+ name.parameterize.strip
+ end
+
+ return if self.code.blank?
+
+ # ensure code is unique
+ self.code = [name.parameterize, rand(9999)].join('-') while Spree::Store.with_deleted.where(code: self.code).exists?
+ end
end
end
diff --git a/core/spec/models/spree/store_spec.rb b/core/spec/models/spree/store_spec.rb
index 9d451fd103..0c4353394d 100644
--- a/core/spec/models/spree/store_spec.rb
+++ b/core/spec/models/spree/store_spec.rb
@@ -112,6 +112,41 @@ describe Spree::Store, type: :model do
end
context 'Callbacks' do
+ describe '#set_code' do
+ let(:store) { build(:store, name: 'Store', code: nil) }
+
+ it 'sets the code' do
+ expect { store.valid? }.to change(store, :code).from(nil).to('store')
+ end
+
+ context 'when code is already set' do
+ let(:store) { build(:store, name: 'Store', code: 'store') }
+
+ it 'does not change the code' do
+ expect { store.valid? }.not_to change(store, :code)
+ end
+ end
+
+ context 'when name is not set' do
+ let(:store) { build(:store, name: nil, code: nil) }
+
+ it 'does not set the code' do
+ expect { store.valid? }.not_to change(store, :code)
+ end
+ end
+
+ context 'when code is already taken' do
+ let(:default_store) { Spree::Store.default }
+ let(:store) { build(:store, name: 'Store', code: default_store.code) }
+
+ it 'generates a new code' do
+ expect { store.valid? }.to change(store, :code)
+ expect(store.code).not_to eq(default_store.code)
+ expect(store.code).to match(/store-\d+/)
+ end
+ end
+ end
+
describe '#ensure_default_taxonomies_are_created' do
let(:store) { build(:store) }
@@ -166,20 +201,18 @@ describe Spree::Store, type: :model do
end
context 'Validations' do
- describe 'code uniqueness' do
- context 'selected code was already used in a deleted store' do
- let(:store_code) { 'store_code' }
+ describe '#code' do
+ let(:default_store) { Spree::Store.default }
- let!(:default_store) { create(:store) }
- let!(:deleted_store) { create(:store, code: store_code).destroy! }
-
- it 'does not cause error related to unique constrains in DB' do
- expect { create(:store, code: store_code) }.not_to raise_error(ActiveRecord::RecordNotUnique)
- end
+ it 'cannot create 2 stores with the same code' do
+ new_store = create(:store, name: default_store.code)
+ expect(new_store.persisted?).to be(true)
+ expect(new_store.code).not_to eq(default_store.code) # code is generated
+ end
- it 'shows accurate validation error' do
- expect { create(:store, code: store_code) }.to raise_error(ActiveRecord::RecordInvalid, 'Validation failed: Code has already been taken')
- end
+ it 'cannot create a store with reserved code' do
+ new_store = build(:store, code: 'admin')
+ expect(new_store.valid?).to be(false)
end
end
end
@@ -423,7 +456,7 @@ describe Spree::Store, type: :model do
end
describe '#checkout_zone_or_default' do
- subject { described_class.new }
+ subject { build(:store) }
context do
include_context 'with checkout zone set'