Repository navigation
docs: explain state machine events in the state machine guide - #2685
Daniel Kirkland (d-kirkland) wants to merge 1 commit into
Conversation
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Developer Docs healthcheckStatus: Completed with |
|
Agent 00-Docs (an AI agent, posting from the PR author's account). This PR adds a "Reacting to state changes" section to the state machine guide: the order of the dispatched events, the How it was checked: the text was drafted from the Shopware platform source at tag A person still needs to confirm the content. Please say if a statement is wrong or if this belongs on a different page. |
| It will return an array of possible actions. | ||
| If your order was still "open", this would result in two entries: One for `cancel` and one for `process`. | ||
|
|
||
| ## Reacting to state changes |
There was a problem hiding this comment.
Reacting to order state changes is a real plugin question, but this section has it upside down. It leads with the raw state_machine.*_changed events (two calls per change, filter side and state yourself) and only gets to state_enter.* at the very end, as a caveat. Those are what a plugin should subscribe to for order, delivery and payment states: same names as the flow triggers and app webhooks, one call, and the order comes loaded.
'state_enter.order_delivery.state.shipped' => 'onDeliveryShipped',
public function onDeliveryShipped(OrderStateMachineStateChangeEvent $event): void
{
$order = $event->getOrder();
}I'd cut the section down to that, a link to the flow reference for the full list, and StateMachineTransitionEvent (see below) for everything else. Keep the sentence about no-op transitions; dispatch order, getter lists and internals can go.
|
|
||
| For each state change, the registry dispatches three events in this order: | ||
|
|
||
| 1. A `Shopware\Core\System\StateMachine\Event\StateMachineTransitionEvent`, dispatched without an explicit event name. It provides `getEntityName()`, `getEntityId()`, `getFromPlace()`, `getToPlace()`, `getContext()`, and `getInternalComment()`. |
There was a problem hiding this comment.
you list the getters but not how to subscribe, and the description says the source doesn't settle that. It does: Symfony falls back to the class name, and core subscribes to StateMachineTransitionEvent::class itself (OrderStockSubscriber, CustomerMetaFieldSubscriber). That's the useful part: one call per transition of any state machine, with entity id and from/to state.
| `StateMachineStateChangeEvent::getStateEventName()` returns a name built from the transition side, the state machine, and the state name, for example `state_enter.order.state.completed`. | ||
| This isn't the name the `StateMachineStateChangeEvent` is dispatched under. | ||
|
|
||
| Instead, the core `OrderStateChangeEventListener` subscribes to the three `state_machine.*_changed` events listed above and dispatches a `Shopware\Core\Checkout\Order\Event\OrderStateMachineStateChangeEvent` under the name returned by `getStateEventName()`. |
There was a problem hiding this comment.
OrderStateChangeEventListener is @internal, please keep it out of the docs. The contract is all a plugin needs: state_enter.<machine>.<state> and state_leave.<machine>.<state> come with an OrderStateMachineStateChangeEvent.
| Instead, the core `OrderStateChangeEventListener` subscribes to the three `state_machine.*_changed` events listed above and dispatches a `Shopware\Core\Checkout\Order\Event\OrderStateMachineStateChangeEvent` under the name returned by `getStateEventName()`. | ||
| A listener for a name such as `state_enter.order_delivery.state.shipped` therefore receives an `OrderStateMachineStateChangeEvent`, which provides the order through `getOrder()`, not a `StateMachineStateChangeEvent`. | ||
| In the 6.7 source, this listener only handles the `order.state`, `order_delivery.state`, and `order_transaction.state` machines. | ||
| The same listener also registers the `state_enter` and `state_leave` names of the states it reads from the state repository as business events of the `OrderStateMachineStateChangeEvent` class, even though it dispatches these events only for the three order state machines. |
There was a problem hiding this comment.
So the capture and refund triggers never fire? Nothing in core dispatches them, but Flow Builder offers them and the flow reference says "Triggers when a payment capture is fully completed". That's a bug to fix in core, not a sentence for a plugin guide. Please drop it.
There was a problem hiding this comment.
Opened a fix PR for this: shopware/shopware#22434
Please remove this restriction when it's merged.
|
Daniel Kirkland (@d-kirkland) - you want to give this a go for correction based on Max's feedback ? |
Summary
state_machine.<machine>_changedfor the three order machines. It also shows how to usegetTransitionSide()so that logic doesn't run twice.state_enter.*andstate_leave.*names carry anOrderStateMachineStateChangeEvent, sent again by the core order listener for the three order machines only.Related links
guides/plugins/plugins/checkout/order/using-the-state-machine.md(edited)v6.7.15.1), the lines each statement is based on:StateMachineRegistry.phpL133-L135StateMachineRegistry.phpL175-L177StateMachineRegistry.phpL252-L254StateMachineTransitionEvent.phpL51-L51StateMachineRegistry.phpL263-L265StateMachineRegistry.phpL277-L279StateMachineRegistry.phpL286-L289StateMachineStateChangeEvent.phpL39-L41OrderStates.phpL10-L10OrderDeliveryStates.phpL10-L10OrderTransactionStates.phpL10-L10OrderStateChangeEventListener.phpL51-L53StateMachineStateChangeEvent.phpL16-L17StateMachineStateChangeEvent.phpL79-L81StateMachineStateChangeEvent.phpL32-L35StateMachineStateChangeEvent.phpL84-L86OrderDeliveryStates.phpL13-L13StateMachineStateChangeEvent.phpL44-L46OrderStateChangeEventListener.phpL159-L162OrderStateChangeEventListener.phpL111-L111OrderStateMachineStateChangeEvent.phpL41-L41OrderStateChangeEventListener.phpL143-L143OrderStateChangeEventListener.phpL125-L128Checklist
PageRefreferences where relevant. (Names and values were checked against the source at the tag linked above.).gitbook.yamlif pages were moved, renamed, or deleted. (N/A, no page moved.).wordlist.txt(and sorted it) if spellcheck flags new legitimate terms. (Check needed if CI Spellcheck flags a word.)Notes
v6.7.15.1):src/Core/Checkout/Order/Aggregate/OrderDelivery/OrderDeliveryStates.phpL10-L10;src/Core/Checkout/Order/Aggregate/OrderDelivery/OrderDeliveryStates.phpL13-L13;src/Core/Checkout/Order/Aggregate/OrderTransaction/OrderTransactionStates.phpL10-L10;src/Core/Checkout/Order/Event/OrderStateMachineStateChangeEvent.phpL41-L41;src/Core/Checkout/Order/Listener/OrderStateChangeEventListener.phpL111-L111;src/Core/Checkout/Order/Listener/OrderStateChangeEventListener.phpL125-L128;src/Core/Checkout/Order/Listener/OrderStateChangeEventListener.phpL143-L143;src/Core/Checkout/Order/Listener/OrderStateChangeEventListener.phpL159-L162;src/Core/Checkout/Order/Listener/OrderStateChangeEventListener.phpL51-L53;src/Core/Checkout/Order/OrderStates.phpL10-L10;src/Core/System/StateMachine/Event/StateMachineStateChangeEvent.phpL16-L17;src/Core/System/StateMachine/Event/StateMachineStateChangeEvent.phpL32-L35;src/Core/System/StateMachine/Event/StateMachineStateChangeEvent.phpL39-L41;src/Core/System/StateMachine/Event/StateMachineStateChangeEvent.phpL44-L46;src/Core/System/StateMachine/Event/StateMachineStateChangeEvent.phpL79-L81;src/Core/System/StateMachine/Event/StateMachineStateChangeEvent.phpL84-L86;src/Core/System/StateMachine/Event/StateMachineTransitionEvent.phpL51-L51;src/Core/System/StateMachine/StateMachineRegistry.phpL133-L135;src/Core/System/StateMachine/StateMachineRegistry.phpL175-L177;src/Core/System/StateMachine/StateMachineRegistry.phpL252-L254;src/Core/System/StateMachine/StateMachineRegistry.phpL263-L265;src/Core/System/StateMachine/StateMachineRegistry.phpL277-L279;src/Core/System/StateMachine/StateMachineRegistry.phpL286-L289.StateMachineTransitionEventwhen no name is given, or how to subscribe to it; Whetherstate_enter.*orstate_leave.*events are dispatched for other machines such asorder_transaction_capture.state; Whether registering a business event makes it show up as a Flow Builder trigger; The order of listeners beyond the leave-then-enter dispatch order.main(Shopware 6.7) only.🤖 Generated with Claude Code