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.

circle-info

To get an overview of how state machines are structured and used across Sylius resources (like orders, shipments, and payments), refer to the State Machine Architecture Guide in the Sylius Book.

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::GRAPH

Explore 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.

circle-info

Best Practice: Always reference workflows, states, and transitions using constants (e.g., OrderCheckoutTransitions::GRAPH) rather than raw strings. This ensures better maintainability and fewer typos. You can find all the workflow constants herearrow-up-right.

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:

circle-check

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

Step 1: Create RemoveSkipShippingTransitionCompilerPass.php


Example 2: Remove the shipping_skipped State

Step 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:

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 Machinearrow-up-right, refer to its documentation for configuring transitions differently or to Sylius 1.x documentationarrow-up-right.

Last updated

Was this helpful?