Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix order checkout flow completion with custom steps #3950

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/lib/spree/core/state_machines/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def define_state_machine!
end

event :complete do
transition to: :complete, from: :confirm
transition to: :complete, from: klass.checkout_steps.keys.last
end

if states[:payment]
Expand Down
38 changes: 38 additions & 0 deletions core/spec/models/spree/order/state_machine_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,42 @@
end
end
end

describe "#complete" do
context "when the confirm step has been taken out of the checkout flow" do
let!(:payment) { create(:payment, state: 'checkout', order: order) }

before :all do
class Spree::Order
checkout_flow do
go_to_state :address
go_to_state :delivery
go_to_state :payment, if: ->(order) { order.payment_required? }
# confirm step has been removed. Payment is the last step now
end
end
end

after :all do
# restore the standard checkout flow to avoid leaking into other tests
class Spree::Order
checkout_flow do
go_to_state :address
go_to_state :delivery
go_to_state :payment, if: ->(order) { order.payment_required? }
go_to_state :confirm
end
end
end

before do
order.update!(state: 'payment')
end

it 'will properly transition from the last checkout flow state to complete' do
order.complete!
expect(order.state).to eq 'complete'
end
end
end
end
24 changes: 24 additions & 0 deletions guides/source/developers/orders/order-state-machine.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,30 @@ The `complete` state is triggered in one of two ways:
2. Payment is required on the order, and at least the order total has been
received as payment.

## Customizing the checkout flow
The central steps in the checkout flow – `address`, `delivery`, `payment` and
`confirm`, are customizable. For example, you might decide you want skip the
`confirm` step and proceed directly from `payment` to `complete`. To modify your
checkout flow, create a decorator similar to the following:

```ruby
# /app/models/mystore/order_decorator.rb

module MyStore::OrderDecorator
def self.prepended(base)
base.checkout_flow do
go_to_state :address
go_to_state :delivery
go_to_state :payment, if: ->(order) { order.payment_required? }
# NOTE: confirm state is commented and will NOT be part of the flow
# go_to_state :confirm
end
end

Spree::Order.prepend self
end
```

## State criteria

Each order state has criteria that must be met before the state can change. For
Expand Down