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.

Add Product#full_in_stock?.

Sebastian Palma40afc21ee483
diff --git a/api/app/models/spree/api/webhooks/stock_item_decorator.rb b/api/app/models/spree/api/webhooks/stock_item_decorator.rb
index 689d48ddb8..b1d2ae1715 100644
--- a/api/app/models/spree/api/webhooks/stock_item_decorator.rb
+++ b/api/app/models/spree/api/webhooks/stock_item_decorator.rb
@@ -20,8 +20,8 @@ module Spree
end
def queue_webhooks_requests_for_product_backorderable!
- product_was_out_of_stock = !product_full_in_stock?
- product_was_not_backorderable = !product_backorderable?
+ product_was_out_of_stock = !product.full_in_stock?
+ product_was_not_backorderable = !product.backorderable?
yield
if product_was_out_of_stock && product_was_not_backorderable && product_backorderable?
touch
@@ -33,11 +33,6 @@ module Spree
Spree::StockItem.exists?(backorderable: true, variant_id: variant.product.variants.ids)
end
- def product_full_in_stock?
- Spree::Product.joins(:variants).merge(Spree::Variant.full_in_stock).
- exists?(id: product.id)
- end
-
def variant_backorderable?
variant.stock_items.exists?(backorderable: true)
end
diff --git a/api/app/models/spree/api/webhooks/stock_movement_decorator.rb b/api/app/models/spree/api/webhooks/stock_movement_decorator.rb
index 14897c61d9..1bfcbfea4d 100644
--- a/api/app/models/spree/api/webhooks/stock_movement_decorator.rb
+++ b/api/app/models/spree/api/webhooks/stock_movement_decorator.rb
@@ -29,17 +29,12 @@ module Spree
end
def queue_webhooks_requests_for_product_back_in_stock!
- product_was_out_of_stock = !product_full_in_stock?
+ product_was_out_of_stock = !product.full_in_stock?
yield
- if product_was_out_of_stock && product_full_in_stock?
+ if product_was_out_of_stock && product.full_in_stock?
product.queue_webhooks_requests!('product.back_in_stock')
end
end
-
- def product_full_in_stock?
- Spree::Product.joins(:variants).merge(Spree::Variant.full_in_stock).
- exists?(id: product.id)
- end
end
end
end
diff --git a/api/spec/models/spree/api/webhooks/stock_item_decorator_spec.rb b/api/spec/models/spree/api/webhooks/stock_item_decorator_spec.rb
index 133b51ca3d..2273f1cb74 100644
--- a/api/spec/models/spree/api/webhooks/stock_item_decorator_spec.rb
+++ b/api/spec/models/spree/api/webhooks/stock_item_decorator_spec.rb
@@ -7,7 +7,10 @@ describe Spree::Api::Webhooks::StockItemDecorator do
let(:stock_location) { variant.stock_locations.first }
describe 'emitting product.backorderable' do
- subject { Timecop.freeze { stock_item.update(backorderable: backorderable) } }
+ subject do
+ product.reload
+ Timecop.freeze { stock_item.update(backorderable: backorderable) }
+ end
let(:body) { Spree::Api::V2::Platform::ProductSerializer.new(product.reload).serializable_hash.to_json }
let(:product) { variant.product }
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index d3a9112130..c9b998c344 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -322,6 +322,10 @@ module Spree
end
end
+ def full_in_stock?
+ variants.full_in_stock.exists?
+ end
+
private
def add_associations_from_prototype
diff --git a/core/spec/models/spree/product_spec.rb b/core/spec/models/spree/product_spec.rb
index a41711ee25..193fe75f67 100644
--- a/core/spec/models/spree/product_spec.rb
+++ b/core/spec/models/spree/product_spec.rb
@@ -824,4 +824,52 @@ describe Spree::Product, type: :model do
it { expect(product.taxons_for_store(store)).to be_a(ActiveRecord::Relation) }
end
+
+ describe '#full_in_stock?' do
+ subject { product.full_in_stock? }
+
+ let!(:product) { create(:product, stores: [store]) }
+ let(:stock_item) { variant.stock_items.first }
+
+ context 'when product has no variants' do
+ it do
+ expect(product.variants).to eq([])
+ expect(subject).to eq(false)
+ end
+ end
+
+ context 'when product has variants' do
+ let!(:variant) { create(:variant, product: product) }
+
+ before { Spree::StockItem.update_all(backorderable: false) }
+
+ context 'when product variant stock items count_on_hand > 0' do
+ before { stock_item.set_count_on_hand(1) }
+
+ it { expect(subject).to eq(true) }
+ end
+
+ context 'when variant stock items count_on_hand <= 0' do
+ before { stock_item.set_count_on_hand(0) }
+
+ it { expect(subject).to eq(false) }
+
+ context 'when variant track_inventory = false' do
+ before { variant.update(track_inventory: false) }
+
+ it { expect(subject).to eq(true) }
+ end
+
+ context 'when variant track_inventory = true' do
+ it { expect(subject).to eq(false) }
+
+ context 'with some variant stock item having backorderable = true' do
+ before { stock_item.update(backorderable: true) }
+
+ it { expect(subject).to eq(true) }
+ end
+ end
+ end
+ end
+ end
end