Shopware 6 Subscriber Beispiel

Einfaches Beispiel für einen simplen Shopware-6-Subscriber

Pfad: MyTheme/src/Subscriber/ProductListingSubscriber.php

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?php

namespace MyTheme\Subscriber;

use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class ProductListingSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        return [
            ProductListingCriteriaEvent::class => 'onProductListingCriteria',
        ];
    }

    public function onProductListingCriteria(ProductListingCriteriaEvent $event): void
    {
        $criteria = $event->getCriteria();
        $criteria->addAssociation('properties');
    }
}

Pfad: src/Resources/config/services.xml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
    <services>
        <service id="MyTheme\Subscriber\ProductListingSubscriber" public="true">
            <tag name="kernel.event_subscriber"/>
        </service>
    </services>
</container>

Nicht vergessen, den Cache zu leeren :)

Erstellt mit Hugo
Theme Stack gestaltet von Jimmy