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.

Exclude discontinued variants from product stock availability

Łukasz Adamczykb771b944315d
diff --git a/spree/core/app/models/spree/product.rb b/spree/core/app/models/spree/product.rb
index 2b38498ddd..e9f7f3ad21 100644
--- a/spree/core/app/models/spree/product.rb
+++ b/spree/core/app/models/spree/product.rb
@@ -594,7 +594,7 @@ module Spree
def any_variant_in_stock_or_backorderable?
if has_variants?
- variants.in_stock_or_backorderable.exists?
+ variants.in_stock_or_backorderable.not_discontinued.exists?
else
default_variant.in_stock_or_backorderable?
end
diff --git a/spree/core/spec/models/spree/product_spec.rb b/spree/core/spec/models/spree/product_spec.rb
index 7cdf879d7e..94e5c2256f 100644
--- a/spree/core/spec/models/spree/product_spec.rb
+++ b/spree/core/spec/models/spree/product_spec.rb
@@ -1126,6 +1126,20 @@ describe Spree::Product, type: :model do
end
end
+ context 'when the only stocked variant is discontinued' do
+ let!(:variant) { create(:variant, product: product) }
+
+ before do
+ Spree::StockItem.update_all(backorderable: false)
+ product.default_variant.stock_items.first.set_count_on_hand(0)
+ variant.stock_items.first.set_count_on_hand(10)
+ variant.update_column(:discontinue_on, 1.day.ago)
+ product.reload
+ end
+
+ it { expect(subject).to eq(false) }
+ end
+
describe '#digital?' do
let(:product) { create(:product, shipping_category: shipping_category) }
let(:shipping_category) { create(:shipping_category) }