SyliusCon 2025
Early Bird Deal
LogoLogo
🛣️ Roadmap💻 Sylius Demo💬 Community Slack
  • Sylius Documentation
  • Sylius Plugins
  • Sylius Stack
  • 📖Sylius 2.0 Documentation
    • Organization
      • Release Cycle
      • Backwards Compatibility Promise
      • Sylius Team
      • Sylius Roadmap
  • Getting Started with Sylius
    • Installation
    • Basic Configuration
    • Shipping & Payment
    • First Product
    • Customizing the Shop
    • Customizing Business Logic
    • Using API
    • Installing Plugins
    • Deployment
    • Summary
  • The Book
    • Introduction to Sylius
    • Installation
      • System Requirements
      • Sylius CE Installation
        • Sylius CE Installation with Docker
      • ➕Sylius Plus Installation
      • Upgrading Sylius CE
      • Upgrading Sylius Plus
    • Architecture
      • Architecture Overview
      • Architectural Drivers
      • Resource Layer
      • State Machine
      • Translations
      • E-Mails
      • Contact
      • Fixtures
      • Events
    • Configuration
      • Channels
      • Locales
      • Currencies
    • Customers
      • Customer & ShopUser
      • ➕Customer Pools
      • AdminUser
      • Addresses
        • Countries
        • Zones
        • Addresses
        • Address Book
    • Products
      • Products
      • Product Reviews
      • Product Associations
      • Attributes
      • Pricing
      • Catalog Promotions
      • Taxons
      • Inventory
      • ➕Multi-Source Inventory
      • Search
    • Carts & Orders
      • Orders
      • Cart flow
      • Taxation
      • Adjustments
      • Cart Promotions
      • Coupons
      • Payments
      • 🧩Invoices
      • Shipments
    • Support
    • Contributing
      • Contributing Code
        • Submitting a Patch
        • ⚠️Security Issues
        • Coding Standards
        • Conventions
        • Sylius License and Trademark
      • Contributing Translations
      • Key Contributors
  • The Customization Guide
    • Customizing Models
      • How to add a custom model?
      • How to add a custom translatable model?
    • Customizing Forms
      • How to add a live form for a custom model?
    • Customizing Styles
    • Customizing Validation
    • Customizing Menus
    • Customizing Templates
    • Customizing Translations
    • Customizing Flashes
    • Customizing State Machines
    • Customizing Grids
    • Customizing Fixtures
    • Customizing API
    • Customizing Serialization of API
    • Customizing Payments
      • How to integrate a Payment Gateway as a Plugin?
  • 🧑‍🍳The Cookbook
  • How to resize images?
  • How to add one image to an entity?
  • How to add multiple images to an entity?
  • Sylius 1.X Documentation
    • 📓Sylius 1.x Documentation
Powered by GitBook
LogoLogo

Developer

  • Community
  • Online Course

About

  • Team

© 2025 Sylius. All Rights Reserved

On this page
  • What are fixtures?
  • Fixture Basics
  • Why Customize Fixtures?
  • How to modify the existing Sylius fixtures?
  • Example: Modifying the Shop Configuration
  • Customizing Fixtures for Extended Models
  • Learn more

Was this helpful?

Edit on GitHub
  1. The Customization Guide

Customizing Fixtures

What are fixtures?

In Sylius, fixtures are plain PHP objects used to initialize or modify your application's state. They're especially useful for:

  • Populating the database with entities (e.g., products, customers)

  • Uploading files

  • Dispatching events

  • Preparing the environment for testing or development

Fixtures can do anything needed to set up your system’s state.

Fixture Basics

A fixture must implement the Sylius\Bundle\FixturesBundle\Fixture\FixtureInterface and be tagged with sylius_fixtures.fixture to be recognized in a fixture suite.

sylius_fixtures:
    suites:
        my_suite_name:
            fixtures:
                my_fixture:  # Fixture key
                    priority: 0  # Lower numbers run later
                    options: ~   # Options passed to the fixture

Note: This interface extends the ConfigurationInterface, known from Configuration classes inside DependencyInjection directories in Symfony bundles.

Why Customize Fixtures?

There are two primary reasons to customize fixtures:

  1. Development & Testing – to preload data for QA environments or demo setups.

  2. Production Initialization – to define initial shop configuration (channels, currencies, methods, etc.).

The default Sylius fixture suite is tailored to a fashion store. Customizing fixtures is recommended if your business sells different products (e.g., books, food).

How to modify the existing Sylius fixtures?

Listing Existing Fixtures

To view available fixtures in your project, run:

php bin/console sylius:fixtures:list

Example: Modifying the Shop Configuration

Create a config/packages/sylius_fixtures.yaml to define your shop setup. Here's an example:

sylius_fixtures:
    suites:
        default: # Used by the `sylius:fixtures:load` command
            fixtures:
                currency:
                    options:
                        currencies: ['CZK', 'HUF']
                channel:
                    options:
                        custom:
                            cz_web_store: # Creating a new channel
                                name: "CZ Web Store"
                                code: "CZ_WEB"
                                locales:
                                    - "%locale%"
                                currencies:
                                    - "CZK"
                                enabled: true
                                hostname: "localhost"
                            hun_web_store:
                                name: "Hun Web Store"
                                code: "HUN_WEB"
                                locales:
                                    - "%locale%"
                                currencies:
                                    - "HUF"
                                enabled: true
                                hostname: "localhost"
                shipping_method:
                    options:
                        custom:
                            ups_eu: # Creating a new shipping method and assigning it to both channels
                                code: "ups_eu"
                                name: "UPS_eu"
                                enabled: true
                                channels:
                                    - "CZ_WEB"
                                    - "HUN_WEB"
                payment_method:
                    options:
                        custom:
                            cash_on_delivery_cz:
                                code: "cash_on_delivery_eu"
                                name: "Cash on delivery_eu"
                                channels:
                                    - "CZ_WEB"
                            bank_transfer:
                                code: "bank_transfer_eu"
                                name: "Bank transfer_eu"
                                channels:
                                    - "CZ_WEB"
                                    - "HUN_WEB"
                                enabled: true

Customizing Fixtures for Extended Models

If you’ve added custom fields to an entity, you'll also need to update the fixture logic. Let's assume that ShippingMethod has been extended with deliveryConditions field.

Scenario: Adding deliveryConditions to ShippingMethod

1. Extend the Example Factory

<?php

namespace App\Fixture\Factory;

use Sylius\Bundle\CoreBundle\Fixture\Factory\ShippingMethodExampleFactory as BaseShippingMethodExampleFactory;
use Sylius\Component\Core\Model\ShippingMethodInterface;
use Sylius\Component\Locale\Model\LocaleInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

final class ShippingMethodExampleFactory extends BaseShippingMethodExampleFactory
{
    // ...
    public function create(array $options = []): ShippingMethodInterface
    {
        $shippingMethod = parent::create($options);

        if (!isset($options['deliveryConditions'])) {
            return $shippingMethod;
        }

        // Access locales through the parent's public API (if available)
        // or find another way to get locales
        foreach ($this->getLocalesFromRepository() as $localeCode) {
            $shippingMethod->setCurrentLocale($localeCode);
            $shippingMethod->setFallbackLocale($localeCode);
            $shippingMethod->setDeliveryConditions($options['deliveryConditions']);
        }

        return $shippingMethod;
    }

    protected function configureOptions(OptionsResolver $resolver): void
    {
        parent::configureOptions($resolver);

        $resolver
            ->setDefault('deliveryConditions', 'some_default_value')
            ->setAllowedTypes('deliveryConditions', ['null', 'string'])
        ;
    }

    private function getLocalesFromRepository(): iterable
    {
        /** @var LocaleInterface[] $locales */
        $locales = $this->localeRepository->findAll();
        foreach ($locales as $locale) {
            yield $locale->getCode();
        }
    }
}

Since Sylius 2.0.8, fixture factory constructor args are protected—easy to extend. On older versions, you must override the full constructor.

  1. Extend the Fixture Class

<?php

// src/Fixture/ShippingMethodFixture.php

namespace App\Fixture;

use Sylius\Bundle\CoreBundle\Fixture\ShippingMethodFixture as BaseShippingMethodFixture;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;

final class ShippingMethodFixture extends BaseShippingMethodFixture
{
    protected function configureResourceNode(ArrayNodeDefinition $resourceNode): void
    {
        parent::configureResourceNode($resourceNode);

        $resourceNode
            ->children()
                ->scalarNode('deliveryConditions')->end()
        ;
    }
}

3. Register Services

Update your config/services.yaml:

services:
    sylius.fixture.example_factory.shipping_method:
        class: App\Fixture\Factory\ShippingMethodExampleFactory
        arguments:
            - "@sylius.factory.shipping_method"
            - "@sylius.repository.zone"
            - "@sylius.repository.shipping_category"
            - "@sylius.repository.locale"
            - "@sylius.repository.channel"
            - "@sylius.repository.tax_category"
        public: true
    
    sylius.fixture.shipping_method:
        class: App\Fixture\ShippingMethodFixture
        arguments:
            - "@sylius.manager.shipping_method"
            - "@sylius.fixture.example_factory.shipping_method"
        tags:
            - { name: sylius_fixtures.fixture }

Disable autowiring for fixtures to avoid duplicate service definitions:

App\:
    resource: '../src/*'
    exclude: '../src/{Entity,Fixture,Migrations,Tests,Kernel.php}'
  1. Use Your Extended Field

Now you can add the deliveryConditions key to your shipping_method fixture in sylius_fixtures.yaml:

sylius_fixtures:
    suites:
        default:
            fixtures:
                shipping_method:
                    options:
                        custom:
                            geis:
                                code: "geis"
                                name: "Geis"
                                enabled: true
                                channels:
                                    - "CZ_WEB"
                                deliveryConditions: "3-5 days"

Learn more

PreviousCustomizing GridsNextCustomizing API

Last updated 11 days ago

Was this helpful?

SyliusFixturesBundle Docs