Skip to content

Commit

Permalink
Spree::Variant.in_stock: Only show distinct variants
Browse files Browse the repository at this point in the history
Without the `.distinct` added to this scope, it returns the same variant
multiple times if there is stock in multiple stock locations.
  • Loading branch information
mamhoff committed Sep 25, 2024
1 parent 7b4addf commit 0a34edd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/app/models/spree/variant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def self.in_stock(stock_locations = nil)
if stock_locations.present?
in_stock_variants = in_stock_variants.where(spree_stock_items: { stock_location_id: stock_locations.map(&:id) })
end
in_stock_variants
in_stock_variants.distinct
end

# Returns a scope of Variants which are suppliable. This includes:
Expand Down
12 changes: 12 additions & 0 deletions core/spec/models/spree/variant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -885,6 +885,18 @@
it "returns all in stock variants" do
expect(subject).to eq [in_stock_variant]
end

context "with stock in several locations" do
let!(:other_stock_location) { create(:stock_location, propagate_all_variants: true) }

before do
Spree::StockItem.where(variant: in_stock_variant).update_all(count_on_hand: 10)
end

it "returns just one variant" do
expect(subject).to eq([in_stock_variant])
end
end
end

context "inventory levels globally not tracked" do
Expand Down

0 comments on commit 0a34edd

Please sign in to comment.