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 checkout for products with untracked inventory (#12724)

Łukasz Adamczykb4bc1e989bb7
diff --git a/admin/spec/controllers/spree/admin/products_controller_spec.rb b/admin/spec/controllers/spree/admin/products_controller_spec.rb
index 3687e15d45..c5a1967c34 100644
--- a/admin/spec/controllers/spree/admin/products_controller_spec.rb
+++ b/admin/spec/controllers/spree/admin/products_controller_spec.rb
@@ -996,13 +996,12 @@ RSpec.describe Spree::Admin::ProductsController, type: :controller do
create(:stock_item, count_on_hand: 20, variant: product.master)
end
- it 'updates stock items' do
+ it 'updates stock item count on hand to 0' do
expect(product.master.stock_items.reload.count).to be > 0
send_request
expect(product.reload.track_inventory).to be(false)
- expect(product.master.stock_items.reload.first.backorderable).to be(true)
expect(product.master.stock_items.reload.first.count_on_hand).to eq(0)
end
end
diff --git a/core/app/models/spree/shipment.rb b/core/app/models/spree/shipment.rb
index 0e76f83dc8..fd67973e4b 100644
--- a/core/app/models/spree/shipment.rb
+++ b/core/app/models/spree/shipment.rb
@@ -430,7 +430,7 @@ module Spree
end
def manifest_unstock(item)
- stock_location.unstock item.variant, item.quantity, self
+ stock_location.unstock(item.variant, item.quantity, self) if item.variant.track_inventory?
end
def recalculate_adjustments
diff --git a/core/app/models/spree/store.rb b/core/app/models/spree/store.rb
index 68d9ba8d82..8d6c94404c 100644
--- a/core/app/models/spree/store.rb
+++ b/core/app/models/spree/store.rb
@@ -341,6 +341,8 @@ module Spree
end
end
+ # Returns the default stock location for the store or creates a new one if it doesn't exist
+ # @return [Spree::StockLocation]
def default_stock_location
@default_stock_location ||= begin
stock_location_scope = Spree::StockLocation.where(default: true)
diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb
index 9d432bf789..18d3fbf154 100644
--- a/core/app/models/spree/variant.rb
+++ b/core/app/models/spree/variant.rb
@@ -73,6 +73,8 @@ module Spree
after_create :create_stock_items
after_create :set_master_out_of_stock, unless: :is_master?
after_commit :clear_line_items_cache, on: :update
+
+ after_create :create_default_stock_item, unless: :track_inventory?
after_update_commit :handle_track_inventory_change
after_commit :remove_prices_from_master_variant, on: [:create, :update], unless: :is_master?
@@ -553,11 +555,17 @@ module Spree
line_items.update_all(updated_at: Time.current)
end
+ def create_default_stock_item
+ return if stock_items.any?
+
+ Spree::Store.current.default_stock_location.set_up_stock_item(self)
+ end
+
def handle_track_inventory_change
return unless track_inventory_previously_changed?
return if track_inventory
- stock_items.update_all(backorderable: true, count_on_hand: 0, updated_at: Time.current)
+ stock_items.update_all(count_on_hand: 0, updated_at: Time.current)
end
def remove_prices_from_master_variant
diff --git a/core/lib/spree/testing_support/factories/stock_item_factory.rb b/core/lib/spree/testing_support/factories/stock_item_factory.rb
index f114758fcb..6a7b5f6c39 100644
--- a/core/lib/spree/testing_support/factories/stock_item_factory.rb
+++ b/core/lib/spree/testing_support/factories/stock_item_factory.rb
@@ -11,6 +11,10 @@ FactoryBot.define do
)&.destroy
end
- after(:create) { |object| object.adjust_count_on_hand(10) }
+ transient do
+ adjust_count_on_hand { true }
+ end
+
+ after(:create) { |object, evaluator| object.adjust_count_on_hand(10) if evaluator.adjust_count_on_hand }
end
end
diff --git a/core/spec/models/spree/shipment_spec.rb b/core/spec/models/spree/shipment_spec.rb
index 618e12e558..13748cb41f 100644
--- a/core/spec/models/spree/shipment_spec.rb
+++ b/core/spec/models/spree/shipment_spec.rb
@@ -604,6 +604,17 @@ describe Spree::Shipment, type: :model do
expect(shipment.stock_location).to receive(:unstock).with(variant, 1, shipment)
shipment.after_resume
end
+
+ context 'for a shipment item that does not track inventory' do
+ before { variant.update(track_inventory: false) }
+
+ it 'skips unstocking the shipment item' do
+ allow(shipment).to receive(:inventory_units).and_return([inventory_unit])
+ shipment.stock_location = create(:stock_location)
+ expect(shipment.stock_location).not_to receive(:unstock)
+ shipment.after_resume
+ end
+ end
end
describe '#ship' do
diff --git a/core/spec/models/spree/stock_movement_spec.rb b/core/spec/models/spree/stock_movement_spec.rb
index ee59c9ce62..e9d5eb1afb 100644
--- a/core/spec/models/spree/stock_movement_spec.rb
+++ b/core/spec/models/spree/stock_movement_spec.rb
@@ -23,6 +23,13 @@ describe Spree::StockMovement, type: :model do
expect(stock_movement).to be_invalid
expect(stock_movement.errors[:quantity]).to include('must be greater than or equal to -10')
end
+
+ it 'allows the negative quantity for a backorderable stock item' do
+ stock_item = create(:stock_item, adjust_count_on_hand: false, backorderable: true)
+ stock_movement = build(:stock_movement, quantity: -1, stock_item: stock_item)
+
+ expect(stock_movement).to be_valid
+ end
end
describe 'Scope' do
diff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rb
index 9c6344fb2d..a1993e77c1 100644
--- a/core/spec/models/spree/variant_spec.rb
+++ b/core/spec/models/spree/variant_spec.rb
@@ -130,6 +130,55 @@ describe Spree::Variant, type: :model do
it { expect(product.master).not_to be_in_stock }
end
end
+
+ describe 'create_default_stock_item' do
+ let(:new_variant) { product.variants.create(track_inventory: track_inventory, is_master: true) }
+
+ context 'when not tracking inventory' do
+ let(:track_inventory) { false }
+
+ it 'creates a default stock item' do
+ new_variant
+
+ expect(new_variant.reload.stock_items.count).to eq(1)
+ expect(new_variant.stock_items[0].count_on_hand).to eq(0)
+ expect(new_variant.stock_items[0].backorderable).to eq(false)
+ end
+
+ context 'when variant is created along with a stock item' do
+ let(:new_variant) do
+ product.variants.create(
+ track_inventory: track_inventory,
+ is_master: true,
+ stock_items_attributes: {
+ '0' => {
+ stock_location_id: create(:stock_location).id,
+ count_on_hand: 10,
+ backorderable: true
+ }
+ }
+ )
+ end
+
+ it 'does not create an another stock item' do
+ new_variant
+
+ expect(new_variant.reload.stock_items.count).to eq(1)
+ expect(new_variant.stock_items[0].count_on_hand).to eq(10)
+ expect(new_variant.stock_items[0].backorderable).to eq(true)
+ end
+ end
+ end
+
+ context 'when tracking inventory' do
+ let(:track_inventory) { true }
+
+ it 'does not create a default stock item' do
+ new_variant
+ expect(new_variant.reload.stock_items.count).to eq(0)
+ end
+ end
+ end
end
describe 'after_update_commit :handle_track_inventory_change' do
@@ -141,13 +190,8 @@ describe Spree::Variant, type: :model do
let!(:variant) { create(:variant, product: product, track_inventory: true) }
let!(:stock_item) { create(:stock_item, variant: variant, count_on_hand: 100) }
- before do
- stock_item.update!(backorderable: false)
- end
-
- it 'updates stock items' do
- expect { subject }.
- to change { stock_item.reload.backorderable }.from(false).to(true).and change { stock_item.reload.count_on_hand }.from(110).to(0)
+ it 'updates stock item count on hand to 0' do
+ expect { subject }.to change { stock_item.reload.count_on_hand }.from(110).to(0)
end
end