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.

Do not allow admin to change the store an order belongs to after the order is marked as complete. Products are scoped by store, call .empty! on the order if store is changed.

Matthew Kennedy62717862655e
diff --git a/backend/app/assets/stylesheets/spree/backend/shared/_base.scss b/backend/app/assets/stylesheets/spree/backend/shared/_base.scss
index 495ab71624..dad04ae3e6 100644
--- a/backend/app/assets/stylesheets/spree/backend/shared/_base.scss
+++ b/backend/app/assets/stylesheets/spree/backend/shared/_base.scss
@@ -44,3 +44,7 @@
color: $primary;
cursor: pointer;
}
+
+#contentHeaderRow {
+ min-height: 51px;
+}
diff --git a/backend/app/views/spree/admin/orders/_store_form.html.erb b/backend/app/views/spree/admin/orders/_store_form.html.erb
index 65e56fb654..44d15aee8a 100644
--- a/backend/app/views/spree/admin/orders/_store_form.html.erb
+++ b/backend/app/views/spree/admin/orders/_store_form.html.erb
@@ -1,18 +1,30 @@
-<div data-hook="store-form-container">
- <%= form_for [:admin, order], method: :put, url: set_store_admin_order_url do |f| %>
- <fieldset>
- <div data-hook="admin_order_store_field">
- <%= f.field_container :store_id, class: ['form-group'] do %>
- <%= f.label :store_id, Spree.t(:store) %>
- <%= f.collection_select(:store_id, @stores, :id, :unique_name, { include_blank: false }, { class: "select2" }) %>
- <%= f.error_message_on :store_id %>
- <% end %>
+<div data-hook="store-form-container col-12">
+ <% if @order.completed? %>
+ <div class="alert alert-warning no-objects-found mt-4">
+ <%= Spree.t('admin.order.once_an_order_is_complete_you_can_not_change_store') %>
</div>
- <div class="form-actions" data-hook="buttons">
- <%= button Spree.t('actions.update'), 'recycle.svg', 'submit', {class: 'btn-success', data: { disable_with: "#{ Spree.t(:saving) }..." }} %>
- <span class="or"><%= Spree.t(:or) %></span>
- <%= button_link_to Spree.t('actions.cancel'), edit_admin_order_url(order), icon: 'cancel.svg' %>
- </div>
- <fieldset>
+ <% else %>
+ <%= form_for [:admin, order], method: :put, url: set_store_admin_order_url do |f| %>
+ <fieldset>
+ <div data-hook="admin_order_store_field" class="row">
+ <%= f.field_container :store_id, class: ['form-group col-12 col-md-6'] do %>
+ <%= f.label :store_id, Spree.t(:store) %>
+ <%= f.collection_select(:store_id, @stores, :id, :unique_name, { include_blank: false }, { class: "select2" }) %>
+ <%= f.error_message_on :store_id %>
+ <% end %>
+
+ <div class="col-12 col-md-6">
+ <div class="alert alert-warning no-objects-found mt-4">
+ <%= Spree.t('admin.order.changing_store_warning') %>
+ </div>
+ </div>
+ </div>
+ <div class="form-actions" data-hook="buttons">
+ <%= button Spree.t('actions.update'), 'recycle.svg', 'submit', {class: 'btn-success', data: { disable_with: "#{ Spree.t(:saving) }..." }} %>
+ <span class="or"><%= Spree.t(:or) %></span>
+ <%= button_link_to Spree.t('actions.cancel'), edit_admin_order_url(order), icon: 'cancel.svg' %>
+ </div>
+ <fieldset>
+ <% end %>
<% end %>
</div>
diff --git a/backend/app/views/spree/admin/shared/_content_header.html.erb b/backend/app/views/spree/admin/shared/_content_header.html.erb
index 148459050b..7e1f9447af 100644
--- a/backend/app/views/spree/admin/shared/_content_header.html.erb
+++ b/backend/app/views/spree/admin/shared/_content_header.html.erb
@@ -1,7 +1,7 @@
<div id="contentHeader" class="row mb-0 pb-0">
<% if content_for?(:page_title) || content_for?(:page_actions) %>
<div class="content-header col <%= "with-page-tabs" if content_for?(:page_tabs) %> page-header">
- <div class="row align-items-center <%= if content_for?(:page_tabs) then "border-bottom pb-2" else "border-bottom pb-2 mb-3" end %>">
+ <div id="contentHeaderRow" class="row align-items-center <%= if content_for?(:page_tabs) then "border-bottom pb-2" else "border-bottom pb-2 mb-3" end %>">
<% if content_for?(:page_title) %>
<h1 class="contextual-title px-0 mb-0 col">
diff --git a/backend/app/views/spree/admin/shared/_order_tabs.html.erb b/backend/app/views/spree/admin/shared/_order_tabs.html.erb
index 46f98e6067..c0275fa4f5 100644
--- a/backend/app/views/spree/admin/shared/_order_tabs.html.erb
+++ b/backend/app/views/spree/admin/shared/_order_tabs.html.erb
@@ -5,6 +5,15 @@
<% content_for :sidebar do %>
<ul class="nav flex-column nav-pills" data-hook="admin_order_tabs">
+ <% if ((can?(:update, @order) && can?(:read, Spree::Store)) && (@order.shipments.size.zero? || @order.shipments.shipped.size.zero?)) && [email protected]? %>
+ <li data-hook='admin_order_tabs_store_details'>
+ <%= link_to_with_icon 'store.svg',
+ Spree.t(:store),
+ store_admin_order_url(@order),
+ class: "#{'active' if current == :store} nav-link" %>
+ </li>
+ <% end %>
+
<% if ((can? :update, @order) && (@order.shipments.size.zero? || @order.shipments.shipped.size.zero?)) %>
<li data-hook='admin_order_tabs_cart_details'>
<%= link_to_with_icon 'cart.svg',
@@ -14,15 +23,6 @@
</li>
<% end %>
- <% if ((can?(:update, @order) && can?(:read, Spree::Store)) && (@order.shipments.size.zero? || @order.shipments.shipped.size.zero?)) %>
- <li data-hook='admin_order_tabs_store_details'>
- <%= link_to_with_icon 'store.svg',
- Spree.t(:store),
- store_admin_order_url(@order),
- class: "#{'active' if current == :store} nav-link" %>
- </li>
- <% end %>
-
<% if can? :update, @order %>
<li data-hook='admin_order_tabs_channel_details'>
<%= link_to_with_icon 'split.svg',
diff --git a/core/app/models/spree/address.rb b/core/app/models/spree/address.rb
index cd42739c6e..8d05bf2629 100644
--- a/core/app/models/spree/address.rb
+++ b/core/app/models/spree/address.rb
@@ -41,7 +41,8 @@ module Spree
validates :phone, if: :require_phone?
end
- validate :state_validate, :postal_code_validate
+ validate :state_validate
+ before_validation :postal_code_validate
validates :label, uniqueness: { conditions: -> { where(deleted_at: nil) },
scope: :user_id,
@@ -223,7 +224,7 @@ module Spree
end
def postal_code_validate
- return if country.blank? || country_iso.blank? || !require_zipcode?
+ return if country.blank? || country_iso.blank? || !require_zipcode? || zipcode.nil?
return unless ::ValidatesZipcode::CldrRegexpCollection::ZIPCODES_REGEX.keys.include?(country_iso.upcase.to_sym)
formatted_zip = ::ValidatesZipcode::Formatter.new(
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb
index c81cfe1457..67555dd761 100644
--- a/core/app/models/spree/order.rb
+++ b/core/app/models/spree/order.rb
@@ -130,6 +130,8 @@ module Spree
before_create :link_by_email
before_update :homogenize_line_item_currencies, if: :currency_changed?
+ before_save :reset_order_if_store_association_has_changed
+
with_options presence: true do
# we want to have this case_sentive: true as changing it to false causes all SQL to use LOWER(slug)
# which is very costly and slow on large set of records
@@ -668,6 +670,14 @@ module Spree
private
+ def reset_order_if_store_association_has_changed
+ return if new_record?
+
+ if store_id_changed?
+ empty!
+ end
+ end
+
def link_by_email
self.email = user.email if user
end
diff --git a/core/config/locales/en.yml b/core/config/locales/en.yml
index 55e43b9d36..42b377ae1c 100644
--- a/core/config/locales/en.yml
+++ b/core/config/locales/en.yml
@@ -571,6 +571,8 @@ en:
return_authorizations: Return Authorizations
customer_returns: Customer Returns
order:
+ changing_store_warning: If you change the store this order belongs to, the order will be reset.
+ once_an_order_is_complete_you_can_not_change_store: 'Sorry, once an order is complete, you can no longer change the store that the order belongs to.'
events:
approve: approve
cancel: cancel