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.

Fix: kept stock propagation within the location's own store and seller

Damian Legawiec3b9e82660bf8
diff --git a/spree/core/app/models/spree/variant.rb b/spree/core/app/models/spree/variant.rb
index ab2104e509..c52a773056 100644
--- a/spree/core/app/models/spree/variant.rb
+++ b/spree/core/app/models/spree/variant.rb
@@ -1194,10 +1194,11 @@ module Spree
end
# Only the product's own store's locations — a new variant must not grow
- # stock items in every other store's warehouses.
+ # stock items in every other store's warehouses — and of those, a seller's
+ # location only stocks what that seller sells.
def create_stock_levels
locations = product&.store ? product.store.stock_locations : StockLocation.all
- locations.where(propagate_all_variants: true).each do |stock_location|
+ locations.where(propagate_all_variants: true, seller_id: [nil, resolved_seller_id].uniq).each do |stock_location|
stock_location.propagate_variant(self)
end
end
diff --git a/spree/core/app/services/spree/stock_locations/stock_items/create.rb b/spree/core/app/services/spree/stock_locations/stock_items/create.rb
index 1b27648e7c..2f0e62e520 100644
--- a/spree/core/app/services/spree/stock_locations/stock_items/create.rb
+++ b/spree/core/app/services/spree/stock_locations/stock_items/create.rb
@@ -10,7 +10,7 @@ module Spree
# @param stock_location [Spree::StockLocation]
# @param variants_scope [ActiveRecord::Relation]
# @return [Spree::ServiceModule::Base::Result]
- def call(stock_location:, variants_scope: Spree::Variant)
+ def call(stock_location:, variants_scope: nil)
Spree::Deprecation.warn('Spree::StockLocations::StockItems::Create is deprecated and will be removed in Spree 6.1. Use Spree::StockLocations::StockLevels::Create instead.')
Spree::StockLocations::StockLevels::Create.call(stock_location: stock_location, variants_scope: variants_scope)
end
diff --git a/spree/core/app/services/spree/stock_locations/stock_levels/create.rb b/spree/core/app/services/spree/stock_locations/stock_levels/create.rb
index 1353bf06e0..de6e5b7694 100644
--- a/spree/core/app/services/spree/stock_locations/stock_levels/create.rb
+++ b/spree/core/app/services/spree/stock_locations/stock_levels/create.rb
@@ -4,7 +4,12 @@ module Spree
class Create
prepend Spree::ServiceModule::Base
- def call(stock_location:, variants_scope: Spree::Variant)
+ # @param stock_location [Spree::StockLocation]
+ # @param variants_scope [ActiveRecord::Relation, nil] defaults to what the
+ # location may stock; always narrowed to it, so no caller can seed rows
+ # for another store's or another seller's variants
+ def call(stock_location:, variants_scope: nil)
+ variants_scope = propagatable_variants(stock_location, variants_scope)
prepared_stock_levels = variants_scope.ids.map do |variant_id|
Hash[
'stock_location_id', stock_location.id,
@@ -19,6 +24,16 @@ module Spree
variants_scope.touch_all
end
end
+
+ private
+
+ # A location stocks its own store's variants, and a seller's location
+ # only the variants that seller sells.
+ def propagatable_variants(stock_location, variants_scope)
+ scope = Spree::Variant.where(id: (variants_scope || Spree::Variant).select(:id))
+ scope = scope.for_seller(stock_location.seller_id) if stock_location.seller_id.present?
+ scope.joins(:product).where(Spree::Product.table_name => { store_id: stock_location.store_id })
+ end
end
end
end
diff --git a/spree/core/spec/models/spree/variant_spec.rb b/spree/core/spec/models/spree/variant_spec.rb
index b49e931d82..0fb9516e59 100644
--- a/spree/core/spec/models/spree/variant_spec.rb
+++ b/spree/core/spec/models/spree/variant_spec.rb
@@ -116,6 +116,16 @@ describe Spree::Variant, type: :model do
create(:variant, product: product)
end
+ it "does not propagate into another seller's location" do
+ seller = create(:seller, store: product.store)
+ location = create(:stock_location, store: product.store, seller: seller, propagate_all_variants: true)
+
+ # Not the factory: it seeds every location itself.
+ variant = product.variants.create!(sku: 'SELLER-PROPAGATION')
+
+ expect(location.stock_levels.where(variant_id: variant.id)).to be_empty
+ end
+
context 'stock location has disable propagate all variants' do
before { Spree::StockLocation.update_all propagate_all_variants: false }
diff --git a/spree/core/spec/services/spree/stock_locations/stock_levels/create_spec.rb b/spree/core/spec/services/spree/stock_locations/stock_levels/create_spec.rb
index 7839c0c386..a0d9c0b12b 100644
--- a/spree/core/spec/services/spree/stock_locations/stock_levels/create_spec.rb
+++ b/spree/core/spec/services/spree/stock_locations/stock_levels/create_spec.rb
@@ -55,12 +55,38 @@ module Spree
end
it 'invalidates the Variant cache' do
- expect(Spree::Variant).to receive(:touch_all).once
- result
+ Timecop.freeze(1.hour.from_now) do
+ expect { result }.to change { unrelated_variant.reload.updated_at }
+ end
end
end
end
+ # Propagation writes rows with insert_all, which skips validations, so
+ # the scope itself has to keep them inside what the location may stock.
+ context 'with variants the location may not stock' do
+ let!(:foreign) { create(:variant, product: create(:product, store: create(:store))) }
+
+ before { stock_location.stock_levels.unscope(:where).delete_all }
+
+ it "leaves another store's variants alone" do
+ result
+
+ expect(stock_location.stock_levels.where(variant_id: foreign.id)).to be_empty
+ end
+
+ it "stocks only the seller's own variants in a seller's location" do
+ seller = create(:seller, store: stock_location.store)
+ own = create(:variant, product: create(:product, seller: seller))
+ stock_location.stock_levels.unscope(:where).delete_all
+ stock_location.update_columns(seller_id: seller.id)
+
+ result
+
+ expect(stock_location.stock_levels.pluck(:variant_id)).to match_array(own.product.variants_including_master.ids)
+ end
+ end
+
context 'without prepared stock levels' do
before { Spree::Variant.delete_all }
@@ -68,11 +94,6 @@ module Spree
expect(stock_location.stock_levels).not_to receive(:insert_all)
result
end
-
- it 'does not invalidates the Variant cache' do
- expect(Spree::Variant).not_to receive(:touch_all)
- result
- end
end
end
end