Skip to main content

Events

Thelia dispatch a lot of events during various workflows : account creation, order process, ...
You can listen any of this event to add or replace logic. Or add your own events that can be listened by other modules.
More info for Event dispatcher component can be found on Symfony documentation

To do this you have to create an event subscriber, it's just a simple class that implement the EventSubscriberInterface with a getSubscribedEvents function to choose what event to listen :

<?php

namespace MyModule\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Thelia\Core\Event\TheliaEvents;

class LogoutListener implements EventSubscriberInterface
{
protected $requestStack;

public function __construct(RequestStack $requestStack)
{
$this->requestStack = $requestStack;
}


public static function getSubscribedEvents($event)
{
// return the subscribed events, their methods and priorities
return [
TheliaEvents::CUSTOMER_LOGOUT => ['onCustomerLogout', 30]
];
}

public function onCustomerLogout()
{
// Do what you want at customer logout
}
}

Native events

Thelia native events are all listed in TheliaEvents class

Propel events

Propel dispatch several events during model lifecycle :

Constant nameDescription
PRE_SAVEBefore persisting the object
POST_SAVEAfter persisting the object
PRE_INSERTBefore inserting to database
POST_INSERTAfter inserting to database
PRE_UPDATEBefore updating the object in database
POST_UPDATEAfter updating the object in database
PRE_DELETEBefore deleting the object in database
POST_DELETEAfter deleting the object in database

Those constants are accessible to the class of model name suffixed by Event.
For example to listen product update use this event ProductEvent::POST_UPDATE

<?php

namespace MyModule\EventListener;

use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Thelia\Model\Event\ProductEvent;

class ProductListener implements EventSubscriberInterface
{
public static function getSubscribedEvents($event)
{
// return the subscribed events, their methods and priorities
return [
ProductEvent::POST_UPDATE => ['postProductUpdate', 30]
];
}

public function postProductUpdate(ProductEvent $event)
{
$productModel = $event->getModel();
// Do what you want with the product
}
}