Skip to content

Commit

Permalink
Merge pull request #3950 from nerfologist/fix-order-checkout-flow-wit…
Browse files Browse the repository at this point in the history
…h-custom-steps

Fix order checkout flow completion with custom steps
  • Loading branch information
kennyadsl authored Apr 30, 2021
2 parents 5590016 + b0cee56 commit 5cbffae
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
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

0 comments on commit 5cbffae

Please sign in to comment.