silvershop-core icon indicating copy to clipboard operation
silvershop-core copied to clipboard

Order Fulfillment State Machine

Open jedateach opened this issue 12 years ago • 9 comments

Storing an order's status in a single Enum db field, as has historically been the case, is to restrictive and simple for accurately storing order statuses. The idea of order 'status' should be shifted to 'state'. A state machine will help keep orders in valid states, and also help us to build systems that properly transition an order's state. State changes can be recorded in a log.

Customers, shop admins, and automatic processes can be allowed to, or prevented from doing certain things, based on the current state. Eg: tracking API can update received status automatically, but only if the order is in a 'dispatched' state.

Some states and events wont matter to store owners, therefore the system needs to be flexible. Devs should be able to incorporate their own steps (states), and transitions in the ordering fulfilment pipeline.

About state machines:

  • http://en.wikipedia.org/wiki/Finite-state_machine
  • http://sourcemaking.com/design_patterns/state
  • http://www.shopify.com/technology/3383012-why-developers-should-be-force-fed-state-machines
  • Spree shopping cart solving a similar issue, but for their checkout: http://spreecommerce.com/blog/upcoming-changes-to-checkout-customization . related docs

Here's a guess at some states that an order might be in:

order status-state flow scenarios

Of course there could be a number of possible shop configurations, that is different types of order fulfilment: http://en.wikipedia.org/wiki/Order_fulfillment

Requirements

Here is a guess at the requirements needed for the shop module:

Add and remove states

Because not every shop workflow is the same, there should be some leeway for adding and removing some states entirely. For example, a shop with only digital products won't have a 'processing' state.

This may be more difficult than it sounds, as I imagine things could get unstable if certain states/transitions don't exist. This could be treated as a nice-to-have for now.

Add transition event callbacks

Ability to assign custom logic to transition events. For example,

Logging all transitions

In conjunction with making notes on an order, updates (state transitions) should also be recorded.

FSM Implementations

There are various state machine implementations around that could be used. Another possible option could be to create a silverstripe module that allows turning any data object into a state machine, allowing for greater reuse.

  • http://pear.github.com/FSM/ (someone blogging about using it)
  • https://github.com/Gignite/state-machine
  • https://github.com/chriswoodford/techne (requires 5.3.3)

Events & Logs

Events that site admins will probably want to record, likely as dates rather than boolean:

  • Order (cart) started (currently Created)
  • Order placed
  • Fully paid
  • Receipt emailed
  • Items packaged
  • Package sent
  • Package delivered
  • Any other events along the way, e.g.:
    • partial payments
    • notes
    • or non-standard changes to an order - this is where an order status log should come in

All this information and events can show up in the CMS to give a clear picture where things are at. Perhaps the events are visualised along a timeline.

Related: #4

jedateach avatar Feb 28 '12 02:02 jedateach

Possibly have a 'financial status', as well as other status like 'fulfillment status'.

jedateach avatar Feb 28 '12 02:02 jedateach

Could also introduce the concept of a line being fulfilled (shipped)...or status per line.

jedateach avatar Feb 28 '12 02:02 jedateach

Documented my thoughts on improving order status functionality.

jedateach avatar Aug 09 '12 04:08 jedateach

Note: the best way to record a time:

SS_Datetime::now()->Rfc2822()

jedateach avatar Aug 09 '12 04:08 jedateach

May be useful: http://code.google.com/p/silverstripe-ecommerce/issues/detail?id=58

jedateach avatar Dec 04 '12 21:12 jedateach

see also: http://sourcemaking.com/design_patterns/state

jedateach avatar Jan 15 '14 21:01 jedateach

Here's a screenshot from prestashop's database-driven custom status setup:

screen shot 2014-12-15 at 2 36 31 pm

jedateach avatar Dec 15 '14 01:12 jedateach

May be useful for inspiration: https://github.com/silverstripe-australia/advancedworkflow

jedateach avatar Dec 15 '14 01:12 jedateach

hello, I have some questions, for example, order system is very common, but how do we store the statemmache status for every order when we use the statemachine in our order system?

LUCKYZHOUSTAR avatar Apr 15 '19 04:04 LUCKYZHOUSTAR