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.

Update Product#in_stock_or_backorderable? and examples.

Sebastian Palmafb097188db38
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 10bde6cef2..f20a77161e 100644
--- a/api/app/models/spree/api/webhooks/stock_item_decorator.rb
+++ b/api/app/models/spree/api/webhooks/stock_item_decorator.rb
@@ -20,7 +20,7 @@ module Spree
end
def queue_webhooks_requests_for_product_backorderable!
- product_was_out_of_stock = !product.full_in_stock?
+ product_was_out_of_stock = !product.any_variant_in_stock_or_backorderable?
product_was_not_backorderable = !product_backorderable?
yield
if product_was_out_of_stock && product_was_not_backorderable && product_backorderable?
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 090f650411..a5e823b351 100644
--- a/api/app/models/spree/api/webhooks/stock_movement_decorator.rb
+++ b/api/app/models/spree/api/webhooks/stock_movement_decorator.rb
@@ -28,9 +28,9 @@ 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.any_variant_in_stock_or_backorderable?
yield
- if product_was_out_of_stock && product.full_in_stock?
+ if product_was_out_of_stock && product.any_variant_in_stock_or_backorderable?
product.queue_webhooks_requests!('product.back_in_stock')
end
end
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb
index f1d4e374c2..d074a90921 100644
--- a/core/app/models/spree/order.rb
+++ b/core/app/models/spree/order.rb
@@ -73,6 +73,7 @@ module Spree
attr_reader :coupon_code
attr_accessor :temporary_address, :temporary_credit_card
+
attribute :state_machine_resumed, :boolean
if Spree.user_class
diff --git a/core/app/models/spree/product.rb b/core/app/models/spree/product.rb
index ff6290a71f..17031fcb74 100644
--- a/core/app/models/spree/product.rb
+++ b/core/app/models/spree/product.rb
@@ -322,8 +322,12 @@ module Spree
end
end
- def full_in_stock?
- variants_including_master.in_stock_or_backorderable.exists?
+ def any_variant_in_stock_or_backorderable?
+ if variants.any?
+ variants_including_master.in_stock_or_backorderable.exists?
+ else
+ master.in_stock_or_backorderable?
+ end
end
private
diff --git a/core/spec/models/spree/product_spec.rb b/core/spec/models/spree/product_spec.rb
index dc3553a193..0421008d14 100644
--- a/core/spec/models/spree/product_spec.rb
+++ b/core/spec/models/spree/product_spec.rb
@@ -825,51 +825,46 @@ 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? }
+ describe '#any_variant_in_stock_or_backorderable?' do
+ subject { product.any_variant_in_stock_or_backorderable? }
let!(:product) { create(:product, stores: [store]) }
let(:stock_item) { variant.stock_items.first }
- context 'when in stock or backorderable variants belong to another product' do
- let!(:another_product_variant) { create(:variant, product: create(:product, stores: [store])) }
-
- it { expect(subject).to eq(false) }
- end
-
- context 'when product has no variants' do
- it do
- expect(product.variants).to eq([])
- expect(subject).to eq(false)
- end
+ context 'when only master variant is in stock or backorderable' do
+ it { expect(subject).to eq(true) }
end
- context 'when product has variants' do
+ context 'with more variants aside from the master variant' do
+ # makes the stock items available for the before hook
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) }
+ context 'with at least one non-master variant stock items count_on_hand > 0' do
+ before do
+ # make all stock items in stock, also for the master variant
+ Spree::StockItem.all.each { |stock_item| stock_item.set_count_on_hand(1) }
+ end
it { expect(subject).to eq(true) }
end
- context 'when variant stock items count_on_hand <= 0' do
+ context 'when all non-master variant stock items have 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
+ context 'when all non-master variant stock items have track_inventory = false' do
before { variant.update(track_inventory: false) }
it { expect(subject).to eq(true) }
end
- context 'when variant track_inventory = true' do
+ context 'when all non-master variant stock items have track_inventory = true' do
it { expect(subject).to eq(false) }
- context 'with some variant stock item having backorderable = true' do
+ context 'when all non-master variant stock items have backorderable = true' do
before { stock_item.update(backorderable: true) }
it { expect(subject).to eq(true) }