Skip to content

Commit

Permalink
Fixed: Do not return cancelled orders when fetching incomplete orders (
Browse files Browse the repository at this point in the history
…#12279)

This prevents from returning an incomplete cancelled order. Scenarios like this can happen during quick checkouts eg. apple pay
  • Loading branch information
damianlegawiec authored Jan 1, 2025
1 parent 9dc05c9 commit 8006701
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion core/app/models/concerns/spree/user_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def self.with_email_or_address(email, address)
end

def last_incomplete_spree_order(store, options = {})
orders.where(store: store).incomplete.
orders.where(store: store).incomplete.not_canceled.
includes(options[:includes]).
order('created_at DESC').
first
Expand Down
4 changes: 2 additions & 2 deletions core/lib/spree/core/controller_helpers/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def set_current_order
return unless try_spree_current_user && current_order

orders_scope = try_spree_current_user.orders.
incomplete.
incomplete.not_canceled.
where.not(id: current_order.id).
where(store_id: current_store.id)

Expand Down Expand Up @@ -86,7 +86,7 @@ def find_order_by_token_or_user(options = {}, with_adjustments = false)
includes = options[:includes] ? order_includes : {}

# Find any incomplete orders for the token
incomplete_orders = current_store.orders.incomplete.includes(includes)
incomplete_orders = current_store.orders.incomplete.not_canceled.includes(includes)

token_order_params = current_order_params.except(:user_id)
order = if with_adjustments
Expand Down
6 changes: 6 additions & 0 deletions core/spec/models/spree/concerns/user_methods_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@
it { is_expected.to eq last_incomplete_order }
end

context 'with incomplete canceled order' do
let(:canceled_order) { create(:order, user: test_user, created_at: 1.day.ago, store: current_store, state: 'canceled') }

it { is_expected.to be_nil }
end

context 'without an incomplete order' do
it { is_expected.to be_nil }
end
Expand Down
5 changes: 5 additions & 0 deletions core/spec/models/spree/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def compute(_computable)
let!(:user) { create(:user) }
let!(:completed_order) { create(:order, user: user, completed_at: Time.current) }
let!(:incompleted_order) { create(:order, user: user, completed_at: nil) }
let!(:canceled_order) { create(:order, user: user, completed_at: nil, state: 'canceled') }

describe '.complete' do
it { expect(Spree::Order.complete).to include completed_order }
Expand All @@ -29,6 +30,10 @@ def compute(_computable)
it { expect(Spree::Order.incomplete).to include incompleted_order }
it { expect(Spree::Order.incomplete).not_to include completed_order }
end

describe '.not_canceled' do
it { expect(Spree::Order.not_canceled).not_to include canceled_order }
end
end

describe '#update_with_updater!' do
Expand Down

0 comments on commit 8006701

Please sign in to comment.