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.

Fixed: Multi Store Admin - editing product in a different store remov… (#13219)

Damian Legawiec863c0656b70a
diff --git a/admin/spec/controllers/spree/admin/products_controller_spec.rb b/admin/spec/controllers/spree/admin/products_controller_spec.rb
index 98feaea858..41b85fceb0 100644
--- a/admin/spec/controllers/spree/admin/products_controller_spec.rb
+++ b/admin/spec/controllers/spree/admin/products_controller_spec.rb
@@ -1091,6 +1091,75 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
end
end
+ context 'with multi-store taxon preservation' do
+ let(:other_store) { create(:store) }
+ let(:store_taxonomy) { create(:taxonomy, store: store) }
+ let(:other_store_taxonomy) { create(:taxonomy, store: other_store) }
+ let(:store_taxon) { create(:taxon, taxonomy: store_taxonomy, name: 'Current Store Category') }
+ let(:new_store_taxon) { create(:taxon, taxonomy: store_taxonomy, name: 'New Store Category') }
+ let(:other_store_taxon) { create(:taxon, taxonomy: other_store_taxonomy, name: 'Other Store Category') }
+
+ before do
+ product.update(stores: [store, other_store])
+ product.taxons << [store_taxon, other_store_taxon]
+ end
+
+ let(:product_params) do
+ {
+ name: 'Updated Product',
+ taxon_ids: [new_store_taxon.id]
+ }
+ end
+
+ it 'preserves taxons from other stores when updating' do
+ expect(product.taxons).to include(store_taxon, other_store_taxon)
+
+ send_request
+
+ product.reload
+ expect(product.taxons).to include(other_store_taxon)
+ expect(product.taxons).to include(new_store_taxon)
+ expect(product.taxons).not_to include(store_taxon)
+ end
+
+ context 'when removing all taxons from current store' do
+ let(:product_params) do
+ {
+ name: 'Updated Product',
+ taxon_ids: ['']
+ }
+ end
+
+ it 'preserves taxons from other stores' do
+ send_request
+
+ product.reload
+ expect(product.taxons).to include(other_store_taxon)
+ expect(product.taxons).not_to include(store_taxon)
+ end
+ end
+
+ context 'with multiple other stores' do
+ let(:third_store) { create(:store) }
+ let(:third_store_taxonomy) { create(:taxonomy, store: third_store) }
+ let(:third_store_taxon) { create(:taxon, taxonomy: third_store_taxonomy, name: 'Third Store Category') }
+
+ before do
+ product.stores << third_store
+ product.taxons << third_store_taxon
+ end
+
+ it 'preserves taxons from all other stores' do
+ send_request
+
+ product.reload
+ expect(product.taxons).to include(other_store_taxon, third_store_taxon, new_store_taxon)
+ expect(product.taxons).not_to include(store_taxon)
+ expect(product.taxons.count).to eq 3
+ end
+ end
+ end
+
it 'will successfully update product' do
send_request
expect(flash[:success]).to eq("Product #{product.name.inspect} has been successfully updated!")
diff --git a/core/app/services/spree/products/prepare_nested_attributes.rb b/core/app/services/spree/products/prepare_nested_attributes.rb
index 35c2e945ac..cc0313b50c 100644
--- a/core/app/services/spree/products/prepare_nested_attributes.rb
+++ b/core/app/services/spree/products/prepare_nested_attributes.rb
@@ -1,5 +1,21 @@
module Spree
module Products
+ # Prepares nested attributes for product updates, handling multi-store scenarios
+ # and permissions.
+ #
+ # This service ensures that when editing a product in one store, taxon associations
+ # from other stores are preserved. This prevents accidental data loss when a store
+ # admin updates product categories in their store without affecting other stores.
+ #
+ # @example
+ # service = Spree::Products::PrepareNestedAttributes.new(
+ # product,
+ # current_store,
+ # params,
+ # current_ability
+ # )
+ # prepared_params = service.call
+ #
class PrepareNestedAttributes
def initialize(product, store, params, ability)
@product = product
@@ -60,6 +76,12 @@ module Spree
# ensure there is at least one store
params[:store_ids] = [store.id] if params[:store_ids].blank?
+ # Preserve taxon associations from other stores
+ # Only merge taxon_ids from other stores if taxon_ids are being updated
+ if params.key?(:taxon_ids)
+ params[:taxon_ids] = merge_taxons_from_other_stores(params[:taxon_ids])
+ end
+
# Add empty list for option_type_ids and mark variants as removed if there are no variants and options
if params[:variants_attributes].blank? && variants_to_remove.any? && !params.key?(:option_type_ids)
params[:option_type_ids] = []
@@ -133,6 +155,28 @@ module Spree
attributes
end
+ # Merges taxon IDs from other stores with submitted taxon IDs from current store.
+ #
+ # This prevents the loss of taxon associations from other stores when a product
+ # is edited in one store. Each store's taxonomy is independent, so editing
+ # categories in Store A should not affect categories in Store B.
+ #
+ # @param submitted_taxon_ids [Array<String>] Taxon IDs from the current store
+ # @return [Array<String>] Combined unique taxon IDs
+ def merge_taxons_from_other_stores(submitted_taxon_ids)
+ return submitted_taxon_ids if product.new_record?
+
+ # Get taxon IDs from other stores that should be preserved
+ other_stores_taxon_ids = product.taxons
+ .joins(:taxonomy)
+ .where.not(spree_taxonomies: { store_id: store.id })
+ .pluck(:id)
+ .map(&:to_s)
+
+ # Merge with submitted taxon IDs from current store and remove duplicates
+ (submitted_taxon_ids + other_stores_taxon_ids).uniq
+ end
+
def update_option_value_variants(option_value_params, existing_variant)
return {} unless option_value_params.present?
return {} unless can_manage_option_types?
diff --git a/core/spec/services/spree/products/prepare_nested_attributes_spec.rb b/core/spec/services/spree/products/prepare_nested_attributes_spec.rb
new file mode 100644
index 0000000000..bd9d4aed6f
--- /dev/null
+++ b/core/spec/services/spree/products/prepare_nested_attributes_spec.rb
@@ -0,0 +1,304 @@
+require 'spec_helper'
+
+RSpec.describe Spree::Products::PrepareNestedAttributes do
+ subject(:prepared_params) { described_class.new(product, store, params, ability).call }
+
+ let(:ability) { Spree::Ability.new(nil) }
+ let(:store) { create(:store) }
+ let(:other_store) { create(:store) }
+ let(:product) { create(:product, stores: [store, other_store]) }
+ let(:params) { ActionController::Parameters.new(raw_params) }
+ let(:raw_params) { {} }
+
+ before do
+ ability.can :manage, :all
+ end
+
+ describe 'taxon preservation across stores' do
+ let(:store_taxonomy) { create(:taxonomy, store: store) }
+ let(:other_store_taxonomy) { create(:taxonomy, store: other_store) }
+ let(:store_taxon) { create(:taxon, taxonomy: store_taxonomy) }
+ let(:other_store_taxon) { create(:taxon, taxonomy: other_store_taxonomy) }
+
+ context 'when editing a product with taxons from multiple stores' do
+ before do
+ product.taxons << [store_taxon, other_store_taxon]
+ end
+
+ context 'when updating taxon_ids from current store' do
+ let(:new_store_taxon) { create(:taxon, taxonomy: store_taxonomy) }
+ let(:raw_params) do
+ {
+ name: 'Updated Product',
+ taxon_ids: [new_store_taxon.id.to_s]
+ }
+ end
+
+ it 'preserves taxons from other stores' do
+ expect(prepared_params[:taxon_ids]).to include(other_store_taxon.id.to_s)
+ end
+
+ it 'includes the new taxon from current store' do
+ expect(prepared_params[:taxon_ids]).to include(new_store_taxon.id.to_s)
+ end
+
+ it 'removes the old taxon from current store' do
+ expect(prepared_params[:taxon_ids]).not_to include(store_taxon.id.to_s)
+ end
+
+ it 'returns unique taxon IDs' do
+ expect(prepared_params[:taxon_ids].uniq).to eq(prepared_params[:taxon_ids])
+ end
+ end
+
+ context 'when removing all taxons from current store' do
+ let(:raw_params) do
+ {
+ name: 'Updated Product',
+ taxon_ids: []
+ }
+ end
+
+ it 'preserves taxons from other stores' do
+ expect(prepared_params[:taxon_ids]).to include(other_store_taxon.id.to_s)
+ end
+
+ it 'does not include taxons from current store' do
+ expect(prepared_params[:taxon_ids]).not_to include(store_taxon.id.to_s)
+ end
+ end
+
+ context 'when taxon_ids param is not present' do
+ let(:raw_params) do
+ {
+ name: 'Updated Product'
+ }
+ end
+
+ it 'does not add taxon_ids key' do
+ expect(prepared_params).not_to have_key(:taxon_ids)
+ end
+ end
+ end
+
+ context 'when creating a new product' do
+ let(:product) { build(:product, stores: [store]) }
+ let(:raw_params) do
+ {
+ name: 'New Product',
+ taxon_ids: [store_taxon.id.to_s]
+ }
+ end
+
+ it 'does not merge taxons from other stores' do
+ expect(prepared_params[:taxon_ids]).to eq([store_taxon.id.to_s])
+ end
+
+ it 'only includes submitted taxon IDs' do
+ expect(prepared_params[:taxon_ids]).to contain_exactly(store_taxon.id.to_s)
+ end
+ end
+
+ context 'when product has taxons from multiple other stores' do
+ let(:third_store) { create(:store) }
+ let(:third_store_taxonomy) { create(:taxonomy, store: third_store) }
+ let(:third_store_taxon) { create(:taxon, taxonomy: third_store_taxonomy) }
+
+ before do
+ product.stores << third_store
+ product.taxons << [store_taxon, other_store_taxon, third_store_taxon]
+ end
+
+ let(:new_store_taxon) { create(:taxon, taxonomy: store_taxonomy) }
+ let(:raw_params) do
+ {
+ name: 'Updated Product',
+ taxon_ids: [new_store_taxon.id.to_s]
+ }
+ end
+
+ it 'preserves taxons from all other stores' do
+ expect(prepared_params[:taxon_ids]).to include(other_store_taxon.id.to_s, third_store_taxon.id.to_s)
+ end
+
+ it 'includes the new taxon from current store' do
+ expect(prepared_params[:taxon_ids]).to include(new_store_taxon.id.to_s)
+ end
+
+ it 'has exactly 3 taxon IDs' do
+ expect(prepared_params[:taxon_ids].size).to eq(3)
+ end
+ end
+ end
+
+ describe 'store_ids handling' do
+ context 'when store_ids is blank' do
+ let(:raw_params) { { name: 'Product' } }
+
+ it 'adds current store to store_ids' do
+ expect(prepared_params[:store_ids]).to eq([store.id])
+ end
+ end
+
+ context 'when store_ids is present' do
+ let(:raw_params) { { name: 'Product', store_ids: [store.id, other_store.id] } }
+
+ it 'preserves submitted store_ids' do
+ expect(prepared_params[:store_ids]).to eq([store.id, other_store.id])
+ end
+ end
+ end
+
+ describe 'product_properties_attributes handling' do
+ context 'when product property value is blank' do
+ let(:product_property) { create(:product_property, product: product) }
+ let(:raw_params) do
+ {
+ product_properties_attributes: {
+ '0' => {
+ id: product_property.id,
+ value: ''
+ }
+ }
+ }
+ end
+
+ it 'marks the product property for destruction' do
+ expect(prepared_params[:product_properties_attributes]['0']['_destroy']).to eq('1')
+ end
+ end
+
+ context 'when product property value is present' do
+ let(:product_property) { create(:product_property, product: product) }
+ let(:raw_params) do
+ {
+ product_properties_attributes: {
+ '0' => {
+ id: product_property.id,
+ value: 'New Value'
+ }
+ }
+ }
+ end
+
+ it 'does not mark the product property for destruction' do
+ expect(prepared_params[:product_properties_attributes]['0']['_destroy']).to be_nil
+ end
+ end
+ end
+
+ describe 'prices_attributes handling' do
+ let(:variant) { product.master }
+
+ context 'when user cannot update prices' do
+ before do
+ ability.cannot :manage, Spree::Price
+ end
+
+ context 'with variants_attributes' do
+ let(:raw_params) do
+ {
+ variants_attributes: {
+ '0' => {
+ id: variant.id,
+ prices_attributes: {
+ '0' => { amount: '10.00' }
+ }
+ }
+ }
+ }
+ end
+
+ it 'removes prices_attributes from variants' do
+ expect(prepared_params[:variants_attributes]['0']).not_to have_key(:prices_attributes)
+ end
+ end
+
+ context 'with master_attributes' do
+ let(:raw_params) do
+ {
+ master_attributes: {
+ prices_attributes: {
+ '0' => { amount: '10.00' }
+ }
+ }
+ }
+ end
+
+ it 'removes prices_attributes from master' do
+ expect(prepared_params[:master_attributes]).not_to have_key(:prices_attributes)
+ end
+ end
+ end
+
+ context 'when user can update prices' do
+ context 'when price amount is blank' do
+ let(:price) { create(:price, variant: variant) }
+ let(:raw_params) do
+ {
+ variants_attributes: {
+ '0' => {
+ id: variant.id,
+ prices_attributes: {
+ '0' => {
+ id: price.id,
+ amount: ''
+ }
+ }
+ }
+ }
+ }
+ end
+
+ it 'marks the price for destruction' do
+ expect(prepared_params[:variants_attributes]['0'][:prices_attributes]['0']['_destroy']).to eq('1')
+ end
+ end
+ end
+ end
+
+ describe 'stock_items_attributes handling' do
+ let(:variant) { product.master }
+
+ context 'when user cannot update stock items' do
+ before do
+ ability.cannot :manage, Spree::StockItem
+ end
+
+ context 'with variants_attributes' do
+ let(:raw_params) do
+ {
+ variants_attributes: {
+ '0' => {
+ id: variant.id,
+ stock_items_attributes: {
+ '0' => { count_on_hand: 10 }
+ }
+ }
+ }
+ }
+ end
+
+ it 'removes stock_items_attributes from variants' do
+ expect(prepared_params[:variants_attributes]['0']).not_to have_key(:stock_items_attributes)
+ end
+ end
+
+ context 'with master_attributes' do
+ let(:raw_params) do
+ {
+ master_attributes: {
+ stock_items_attributes: {
+ '0' => { count_on_hand: 10 }
+ }
+ }
+ }
+ end
+
+ it 'removes stock_items_attributes from master' do
+ expect(prepared_params[:master_attributes]).not_to have_key(:stock_items_attributes)
+ end
+ end
+ end
+ end
+end