From 0a34edd7c9ec9892a1b4f01df3fe3be805816757 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Wed, 25 Sep 2024 14:09:03 +0200 Subject: [PATCH] Spree::Variant.in_stock: Only show distinct variants Without the `.distinct` added to this scope, it returns the same variant multiple times if there is stock in multiple stock locations. --- core/app/models/spree/variant.rb | 2 +- core/spec/models/spree/variant_spec.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/core/app/models/spree/variant.rb b/core/app/models/spree/variant.rb index dec68365450..1c20b3f7932 100644 --- a/core/app/models/spree/variant.rb +++ b/core/app/models/spree/variant.rb @@ -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: diff --git a/core/spec/models/spree/variant_spec.rb b/core/spec/models/spree/variant_spec.rb index def0b21ff3c..ee8a3f58290 100644 --- a/core/spec/models/spree/variant_spec.rb +++ b/core/spec/models/spree/variant_spec.rb @@ -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