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.

Added `Order#backordered_variants`

Damian Legawiecb5f6480ea4be
diff --git a/core/app/models/spree/order.rb b/core/app/models/spree/order.rb
index 9e23a8871b..757c3cc1fb 100644
--- a/core/app/models/spree/order.rb
+++ b/core/app/models/spree/order.rb
@@ -412,6 +412,15 @@ module Spree
payments.valid.not_store_credits.first&.source
end
+ # Returns the backordered variants for the order
+ #
+ # @return [Array<Spree::Variant>] the backordered variants for the order
+ def backordered_variants
+ variants.
+ joins(:stock_items, :product).
+ where(Spree::StockItem.table_name => { count_on_hand: ..0, backorderable: true })
+ end
+
def can_ship?
complete? || resumed? || awaiting_return? || returned?
end
diff --git a/core/spec/models/spree/order_spec.rb b/core/spec/models/spree/order_spec.rb
index b9df74598e..f4b8eea705 100644
--- a/core/spec/models/spree/order_spec.rb
+++ b/core/spec/models/spree/order_spec.rb
@@ -1958,4 +1958,24 @@ describe Spree::Order, type: :model do
expect(subject).to eq(payment_source)
end
end
+
+ describe '#backordered_variants' do
+ subject { order.backordered_variants }
+
+ let(:order) { create(:order) }
+ let(:variant) { create(:variant) }
+ let(:variant_2) { create(:variant) }
+
+ before do
+ create(:line_item, order: order, variant: variant, quantity: 1)
+ variant.stock_items.first.update(count_on_hand: 0, backorderable: true)
+
+ create(:line_item, order: order, variant: variant_2, quantity: 1)
+ variant_2.stock_items.first.update(count_on_hand: 1, backorderable: true)
+ end
+
+ it 'returns the backordered variants' do
+ expect(subject).to eq([variant])
+ end
+ end
end