Skip stock check when requested quantity is zero

This prevents an error being raised when available stock is negative
This commit is contained in:
Rob Harrington
2018-03-02 12:03:12 +11:00
parent fe4145d9d5
commit 28491ee948
2 changed files with 18 additions and 0 deletions

View File

@@ -131,7 +131,9 @@ Spree::LineItem.class_eval do
# MONKEYPATCH of Spree method
# Enables scoping of variant to hub/shop, so we check stock against relevant overrides if they exist
# Also skips stock check if requested quantity is zero
def sufficient_stock?
return true if quantity == 0 # This line added
scoper.scope(variant) # This line added
return true if Spree::Config[:allow_backorders]
if new_record? || !order.completed?

View File

@@ -76,6 +76,12 @@ module Spree
expect(li.max_quantity).to eq 10
end
it "caps at zero when stock is negative" do
v.update_attributes(on_hand: -2)
li.cap_quantity_at_stock!
expect(li.reload.quantity).to eq 0
end
context "when a variant override is in place" do
let!(:hub) { create(:distributor_enterprise) }
let!(:vo) { create(:variant_override, hub: hub, variant: v, count_on_hand: 2) }
@@ -92,6 +98,16 @@ module Spree
li.cap_quantity_at_stock!
expect(li.quantity).to eq 2
end
context "when count on hand is negative" do
before { vo.update_attributes(count_on_hand: -3) }
it "caps at zero" do
v.update_attributes(on_hand: -2)
li.cap_quantity_at_stock!
expect(li.reload.quantity).to eq 0
end
end
end
end