How to Create an Event Listener in Shopware 6?

Event Listener in Shopware 6

The processes below must be followed in Shopware 6 in order to create an event listener:

Create a new plugin: In Shopware 6, you must first create a new plugin before you can create an event listener. The plugin structure can be manually created or created using the Shopware 6 Plugin Creator.

Shopware 6 contains a number of events that are triggered at various systemic locations. Shopware 6 contains a list of the events which you can see in this blog post. List Of Events In Shopware 6

Make the class that will act as your event listener by creating it inside of your plugin. The Symfony Component Event Dispatcher and Event Subscriber Interface interface should be implemented by this class.

Generating your personal subscriber base

To register a custom subscriber, you need to upload a services.xml file containing your plugin. This can be accomplished by placing a file named services.xml in a directory named src/Resources/config/. If you’re familiar with Symfony subscribers, that’s exactly it.

Defining a new subscriber class

We need to create a class that implements the EventSubscriberInterface before we can begin creating a subscriber. As was already mentioned, a subscriber for Shopware 6 seems exactly the same as it does in Symfony.

Let’s create your first subscriber which will look like this.

File Path: <CustomPlugin>/src/Subscriber/FirstSubscriber.php

<?php declare(strict_types=1);

namespace Custom\First\Subscriber;

use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Content\Product\ProductEvents;

class FirstSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents(): array
    {
        // Return the events to listen to as array like this:  <event to listen to> => <method to execute>
        return [
            ProductEvents::PRODUCT_LOADED_EVENT => 'onProductsLoaded'
        ];
    }

    public function onProductsLoaded(EntityLoadedEvent $event)
    {
        // Do something
        // E.g. work with the loaded entities: $event->getEntities()
    }
}

Once you have created the event listener, you can register it in your plugin’s services.xml file.

File Path: <CustomPlugin>/src/Resources/config/services.xml

<?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="Custom\First\Subscriber">
            <tag name="kernel.event_subscriber"/>
        </service>
    </services>
</container>

Once you have registered the event listener, it will be automatically called whenever the product.loaded event is triggered.

Please contact us at manish@bay20.com or call us at +91-8800519180 for any support related to shopware. You can also visit the Shopware 6 development page to check the services we offer.