Customizing State Machines
Sylius uses Symfony's Workflow Component to manage business processes such as order checkout and payment flows. These workflows (also called state machines) define the states an entity can be in and how transitions between these states happen.
Sylius provides predefined workflows, but many projects require customizations. This guide will walk you through how to find, extend, and modify these workflows in a clear, step-by-step way.
How to find the desired state machine to customize?
Locate the Workflow Graph
Each workflow in Sylius is defined with a unique graph constant. For example, the order checkout workflow uses:
Sylius\Component\Core\OrderCheckoutTransitions::GRAPHExplore Defined Workflows
You can list all configured Sylius workflows using Symfony’s console tool:
php bin/console debug:config framework workflows | grep sylius_This will display all defined workflows, their states (places), and transitions.
Extending the existing workflows
Let’s assume you want to add a new state and a custom transition to the order checkout workflow.
Add a New State
Update your config/packages/_sylius.yaml:
Add a New Transition
Create a transition from an existing state to the new custom_state:
You can now create custom logic triggered by this transition if needed.
Result:
The workflow before:
The workflow after:
To learn how to generate graphs like the ones used in this guide, visit this site.
Removing Transitions and States
Sometimes, you need to remove default transitions or states. This requires a compiler pass to alter Symfony's service container.
Example 1: Remove the skip_shipping Transition
skip_shipping TransitionStep 1: Create RemoveSkipShippingTransitionCompilerPass.php
Example 2: Remove the shipping_skipped State
shipping_skipped StateStep 2: Create RemoveShippingSkippedStateCompilerPass.php
Register Compiler Passes
In your Kernel.php:
Result:
Extending the existing Transitions
By default, Symfony's Workflow component does not merge configuration arrays for transitions, meaning you cannot simply add more from states to an existing transition using the regular workflow configuration in your config/packages files.
To work around this limitation and programmatically extend an existing transition, such as adding new from places to it, you can implement a custom Compiler Pass.
Here is an example of how you can dynamically add new from states to the existing transitions in the sylius_payment state machine.
Create a compiler pass:
Register it in your Kernel.php:
Kernel.php:Result:
Before:
After:
Adding Workflow Callbacks
You can hook into workflow events using Symfony event listeners.
Example: Send an Email After Order Completion
Create a listener class:
Register the service:
Overriding Existing Workflow Listeners
To customize existing logic, redefine the listener service.
Example: Customize Shipping State Resolver
🔧 Debug Your Workflow Setup
Use this command to see what listeners are registered for a workflow event:
Optional: Legacy Winzou State Machine
If you're migrating from Sylius 1.x or using the Winzou State Machine, refer to its documentation for configuring transitions differently or to Sylius 1.x documentation.
Last updated
Was this helpful?
