Skip to content

Commit

Permalink
Count variants only once in Spree::Variant.suppliable
Browse files Browse the repository at this point in the history
Variants are counted multiple times when they have stock in multiple stock
locations. Adding `distinct` fixes this behavior. Fixes solidusio#2868
  • Loading branch information
mayanktap authored and spaghetticode committed Jan 29, 2019
1 parent 748e4b2 commit fe64554
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
2 changes: 1 addition & 1 deletion core/app/models/spree/variant.rb
Expand Up @@ -121,7 +121,7 @@ def self.suppliable
Spree::StockItem.arel_table[:count_on_hand].gt(0),
Spree::StockItem.arel_table[:backorderable].eq(true)
]
joins(:stock_items).where(arel_conditions.inject(:or))
joins(:stock_items).where(arel_conditions.inject(:or)).distinct
end

self.whitelisted_ransackable_associations = %w[option_values product prices default_price]
Expand Down
4 changes: 4 additions & 0 deletions core/spec/models/spree/variant_spec.rb
Expand Up @@ -783,6 +783,10 @@
expect( subject ).not_to include(out_of_stock_variant)
end

it "includes variants only once" do
expect(subject.to_a.count(in_stock_variant)).to be 1
end

context "inventory levels globally not tracked" do
before { Spree::Config.track_inventory_levels = false }

Expand Down

0 comments on commit fe64554

Please sign in to comment.