PHP code example of shopware / fixture-bundle

1. Go to this page and download the library: Download shopware/fixture-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

shopware / fixture-bundle example snippets




declare(strict_types=1);

namespace App\Fixture;

use Shopware\FixtureBundle\FixtureInterface;
use Shopware\FixtureBundle\Attribute\Fixture;

#[Fixture]
class BasicFixture implements FixtureInterface
{
    public function load(): void
    {
        // Your fixture logic here
        echo "Loading basic fixture...\n";
    }
}

#[Fixture(priority: 100)]
class HighPriorityFixture implements FixtureInterface
{
    public function load(): void
    {
        // This runs before fixtures with lower priority
    }
}

#[Fixture(
    priority: 50,
    dependsOn: [CategoryFixture::class]
)]
class ProductFixture implements FixtureInterface
{
    public function load(): void
    {
        // CategoryFixture will always run before this
    }
}

#[Fixture(
    groups: ['test-data', 'products']
)]
class ProductTestDataFixture implements FixtureInterface
{
    public function load(): void
    {
        // This fixture belongs to both 'test-data' and 'products' groups
    }
}



declare(strict_types=1);

namespace App\Fixture;

use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\FixtureBundle\FixtureInterface;
use Shopware\FixtureBundle\Attribute\Fixture;

#[Fixture(
    priority: 100,
    groups: ['catalog', 'test-data']
)]
class CategoryFixture implements FixtureInterface
{
    public function __construct(
        private readonly EntityRepository $categoryRepository,
        private readonly Connection $connection
    ) {
    }

    public function load(): void
    {
        $categories = [
            [
                'id' => Uuid::randomHex(),
                'name' => 'Electronics',
                'active' => true,
            ],
            [
                'id' => Uuid::randomHex(),
                'name' => 'Clothing',
                'active' => true,
            ],
        ];

        foreach ($categories as $category) {
            $this->categoryRepository->create([$category], Context::createDefaultContext());
        }
    }
}



declare(strict_types=1);

namespace App\Fixture;

use Shopware\FixtureBundle\Attribute\Fixture;
use Shopware\FixtureBundle\FixtureInterface;
use Shopware\FixtureBundle\Helper\Theme\ThemeFixtureDefinition;
use Shopware\FixtureBundle\Helper\Theme\ThemeFixtureLoader;

#[Fixture(groups: ['theme-config'])]
class ThemeFixture implements FixtureInterface
{
    public function __construct(
        private readonly ThemeFixtureLoader $themeFixtureLoader
    ) {
    }

    public function load(): void
    {
        $this->themeFixtureLoader->apply(
            (new ThemeFixtureDefinition('Shopware default theme'))
                ->config('sw-color-brand-primary', '#ff6900')
                ->config('sw-border-radius-default', '8px')
                ->config('sw-font-family-base', '"Inter", sans-serif')
                ->config('sw-background-color', '#f8f9fa')
        );
    }
}

#[Fixture(groups: ['theme-config', 'branding'])]
class BrandingThemeFixture implements FixtureInterface
{
    public function __construct(
        private readonly ThemeFixtureLoader $themeFixtureLoader
    ) {
    }

    public function load(): void
    {
        // Configure main storefront theme
        $this->themeFixtureLoader->apply(
            (new ThemeFixtureDefinition('Shopware default theme'))
                ->config('sw-color-brand-primary', '#007bff')
                ->config('sw-color-brand-secondary', '#6c757d')
        );

        // Configure custom theme if available
        try {
            $this->themeFixtureLoader->apply(
                (new ThemeFixtureDefinition('Custom Theme'))
                    ->config('custom-header-color', '#ffffff')
                    ->config('custom-footer-background', '#333333')
            );
        } catch (FixtureException $e) {
            // Custom theme not available, skip
        }
    }
}

#[Fixture(groups: ['theme-config', 'branding'])]
class BrandingThemeFixture implements FixtureInterface
{
    public function __construct(
        private readonly ThemeFixtureLoader $themeFixtureLoader
    ) {
    }

    public function load(): void
    {
        // Will be uploaded just once and reused based on file content
        $logo = $this->mediaHelper->upload(__DIR__ . '/shop.png', $this->mediaHelper->getDefaultFolder(ThemeDefinition::ENTITY_NAME)->getId());
    
        // Configure main storefront theme
        $this->themeFixtureLoader->apply(
            (new ThemeFixtureDefinition('Shopware default theme'))
                ->config('sw-color-brand-primary', '#007bff')
                ->config('sw-color-brand-secondary', '#6c757d')
                ->config('sw-logo-desktop', $logo)
                ->config('sw-logo-tablet', $logo)
                ->config('sw-logo-mobile', $logo)
        );
    }
}



declare(strict_types=1);

namespace Acme\Fixture;

use Shopware\Core\System\CustomField\CustomFieldTypes;
use Shopware\FixtureBundle\Attribute\Fixture;
use Shopware\FixtureBundle\FixtureInterface;
use Shopware\FixtureBundle\Helper\CustomField\CustomFieldFixtureDefinition;
use Shopware\FixtureBundle\Helper\CustomField\CustomFieldSetFixtureDefinition;
use Shopware\FixtureBundle\Helper\CustomField\CustomFieldSetFixtureLoader;

#[Fixture]
class CustomFieldFixture implements FixtureInterface
{
    public function __construct(
        private readonly CustomFieldSetFixtureLoader $customFieldSetFixtureLoader
    ) {
    }

    public function load(): void
    {
        $this->customFieldSetFixtureLoader->apply(
            (new CustomFieldSetFixtureDefinition('Product Specifications', 'product_specs'))
                ->relation('product')
                ->field(
                    (new CustomFieldFixtureDefinition('weight', CustomFieldTypes::FLOAT))
                        ->label('en-GB', 'Weight (kg)')
                        ->label('de-DE', 'Gewicht (kg)')
                        ->placeholder('en-GB', 'Enter product weight')
                        ->helpText('en-GB', 'Product weight in kilograms')
                        ->position(10)
                )
                ->field(
                    (new CustomFieldFixtureDefinition('dimensions', CustomFieldTypes::TEXT))
                        ->label('en-GB', 'Dimensions')
                        ->placeholder('en-GB', 'L x W x H')
                        ->position(20)
                )
                ->field(
                    (new CustomFieldFixtureDefinition('warranty_period', CustomFieldTypes::INT))
                        ->label('en-GB', 'Warranty Period (months)')
                        ->config(['min' => 0, 'max' => 120])
                        ->position(30)
                )
        );
    }
}



declare(strict_types=1);

namespace Acme\Fixture;

use Shopware\FixtureBundle\Attribute\Fixture;
use Shopware\FixtureBundle\FixtureInterface;
use Shopware\FixtureBundle\Helper\Customer\CustomerFixtureDefinition;
use Shopware\FixtureBundle\Helper\Customer\CustomerFixtureLoader;

#[Fixture]
class CustomerFixture implements FixtureInterface
{
    public function __construct(
        private readonly CustomerFixtureLoader $customerFixtureLoader
    ) {
    }

    public function load(): void
    {
        $this->customerFixtureLoader->apply(
            (new CustomerFixtureDefinition('[email protected]'))
                ->firstName('John')
                ->lastName('Doe')
                ->salutation('mr')
                ->password('password123')
                ->company('ACME Corporation')
                ->department('IT Department')
        );
    }
}

#[Fixture(groups: ['customers', 'test-data'])]
class DetailedCustomerFixture implements FixtureInterface
{
    public function __construct(
        private readonly CustomerFixtureLoader $customerFixtureLoader
    ) {
    }

    public function load(): void
    {
        $this->customerFixtureLoader->apply(
            (new CustomerFixtureDefinition('[email protected]'))
                ->firstName('Jane')
                ->lastName('Smith')
                ->salutation('mrs')
                ->title('Dr.')
                ->birthday('1990-05-15')
                ->company('Tech Solutions Ltd')
                ->department('Marketing')
                ->vatId('DE123456789')
                ->password('secure123')
                ->customerNumber('CUST-001')
                ->affiliateCode('PARTNER-123')
                ->campaignCode('SUMMER2024')
                ->active(true)
                ->guest(false)
                ->customFields([
                    'vip_level' => 'gold',
                    'newsletter_subscription' => true,
                    'preferred_contact' => 'email'
                ])
        );
    }
}

#[Fixture(groups: ['customers', 'addresses'])]
class CustomerWithAddressesFixture implements FixtureInterface
{
    public function __construct(
        private readonly CustomerFixtureLoader $customerFixtureLoader
    ) {
    }

    public function load(): void
    {
        $this->customerFixtureLoader->apply(
            (new CustomerFixtureDefinition('[email protected]'))
                ->firstName('Max')
                ->lastName('Mustermann')
                ->salutation('mr')
                ->password('password')
                ->defaultBillingAddress([
                    'firstName' => 'Max',
                    'lastName' => 'Mustermann',
                    'street' => 'Musterstraße 123',
                    'zipcode' => '12345',
                    'city' => 'Musterstadt',
                    'country' => 'DEU',
                    'company' => 'Musterfirma GmbH',
                    'phoneNumber' => '+49 123 456789',
                    'salutation' => 'mr'
                ])
                ->defaultShippingAddress([
                    'firstName' => 'Max',
                    'lastName' => 'Mustermann',
                    'street' => 'Lieferadresse 456',
                    'zipcode' => '67890',
                    'city' => 'Lieferstadt',
                    'country' => 'DEU',
                    'additionalAddressLine1' => 'Building B',
                    'additionalAddressLine2' => '3rd Floor'
                ])
                ->addAddress('work', [
                    'firstName' => 'Max',
                    'lastName' => 'Mustermann',
                    'street' => 'Office Street 789',
                    'zipcode' => '11111',
                    'city' => 'Business City',
                    'country' => 'DEU',
                    'company' => 'Work Corporation'
                ])
        );
    }
}

#[Fixture(groups: ['customers', 'guest-orders'])]
class GuestCustomerFixture implements FixtureInterface
{
    public function __construct(
        private readonly CustomerFixtureLoader $customerFixtureLoader
    ) {
    }

    public function load(): void
    {
        $this->customerFixtureLoader->apply(
            (new CustomerFixtureDefinition('[email protected]'))
                ->firstName('Guest')
                ->lastName('User')
                ->guest(true)
                ->active(false)
                ->defaultBillingAddress([
                    'firstName' => 'Guest',
                    'lastName' => 'User',
                    'street' => 'Guest Street 1',
                    'zipcode' => '99999',
                    'city' => 'Guest City',
                    'country' => 'DEU'
                ])
        );
    }
}