Add Flow Builder trigger
This functionality is available starting with Shopware 6.4.6.0

Overview

In this guide, you'll learn how to create a custom flow trigger in Shopware. Triggers are used by the flow builder. This example will introduce a new custom trigger. The shop owner is then able to define what to do with the new trigger.

Prerequisites

In order to add your own custom flow trigger for your plugin, you first need a plugin as base. Therefore, you can refer to the Plugin Base Guide.
You also should be familiar with Add custom event to know how to create an event. Please refer to the Flow Builder concept for better integration later.

Existing triggers and actions

You can refer to the Flow reference to read triggers and actions detail.

Event interfaces and classes

Any event that implements one of these interfaces will be available in the trigger list of the Flow Builder module in administration. Besides, the event will have the ability to execute the action that belongs to the interface.
  • MailAware: This interface provides MailRecipientStruct and salesChannelId.
  • OrderAware: This interface provides orderId, which is used to add tags, sendmail or generate documents, etc...
  • CustomerAware: This interface same as OrderAware but for customer, which provide customerId, used to add tags, remove tags, sendmail, etc...
  • UserAware: This interface provides userId for all actions related to the user.
  • SalesChannelAware: This interface simply provides salesChannelId.

Create custom flow trigger

To create a custom flow trigger, firstly you have to create a plugin and install it, you can refer to the Plugin Base Guide to do it. I will create a plugin named ExamplePlugin. There will be an example to actually show your new trigger in the administration.

Create a new trigger (event)

In this example, we will name it ExampleEvent to some actions related to customers when dispatching this event. It will be placed in the directory /src/Core/Checkout/Customer/Event. Our new event has to implement Shopware\Core\Framework\Event\CustomerAware interface to enable actions requiring this Aware.
Currently, you will need to also implement Shopware\Core\Framework\Event\BusinessEventInterface; in case the feature flag FEATURE_NEXT_17858 is inactive. Please take note that this interface will be removed in v6.5 .
Below you can find an example implementation:
<plugin root>/src/Core/Checkout/Customer/Event/ExampleEvent.php
1
<?php declare(strict_types=1);
2
3
namespace Swag\ExamplePlugin\Core\Checkout\Customer\Event;
4
5
use Shopware\Core\Checkout\Customer\CustomerDefinition;
6
use Shopware\Core\Checkout\Customer\CustomerEntity;
7
use Shopware\Core\Framework\Context;
8
use Shopware\Core\Framework\Event\CustomerAware;
9
use Shopware\Core\Framework\Event\BusinessEventInterface;
10
use Shopware\Core\Framework\Event\EventData\EntityType;
11
use Shopware\Core\Framework\Event\EventData\EventDataCollection;
12
use Symfony\Contracts\EventDispatcher\Event;
13
14
class ExampleEvent extends Event implements CustomerAware, BusinessEventInterface
15
{
16
public const EVENT_NAME = 'example.event';
17
18
private CustomerEntity $customer;
19
20
private Context $context;
21
22
public function __construct(Context $context, CustomerEntity $customer)
23
{
24
$this->customer = $customer;
25
$this->context = $context;
26
}
27
28
public function getName(): string
29
{
30
return self::EVENT_NAME;
31
}
32
33
public function getCustomer(): CustomerEntity
34
{
35
return $this->customer;
36
}
37
38
public function getCustomerId(): string
39
{
40
return $this->customer->getId();
41
}
42
43
public static function getAvailableData(): EventDataCollection
44
{
45
return (new EventDataCollection())
46
->add('customer', new EntityType(CustomerDefinition::class));
47
}
48
49
public function getContext(): Context
50
{
51
return $this->context;
52
}
53
}
Copied!

Add your new event to the flow trigger list

At this step you need to add your new event to the flow trigger list, let see the code below:
<plugin root>/src/Core/Checkout/Customer/Subscriber/BusinessEventCollectorSubscriber.php
1
<?php declare(strict_types=1);
2
3
namespace Swag\ExamplePlugin\Core\Checkout\Customer\Subscriber;
4
5
use Shopware\Core\Framework\Event\BusinessEventCollector;
6
use Shopware\Core\Framework\Event\BusinessEventCollectorEvent;
7
use Swag\ExamplePlugin\Core\Checkout\Customer\Event\ExampleEvent;
8
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
9
10
class BusinessEventCollectorSubscriber implements EventSubscriberInterface
11
{
12
private BusinessEventCollector $businessEventCollector;
13
14
public function __construct(BusinessEventCollector $businessEventCollector) {
15
$this->businessEventCollector = $businessEventCollector;
16
}
17
18
public static function getSubscribedEvents()
19
{
20
return [
21
BusinessEventCollectorEvent::NAME => ['onAddExampleEvent', 1000],
22
];
23
}
24
25
public function onAddExampleEvent(BusinessEventCollectorEvent $event): void
26
{
27
$collection = $event->getCollection();
28
29
$definition = $this->businessEventCollector->define(ExampleEvent::class);
30
31
if (!$definition) {
32
return;
33
}
34
35
$collection->set($definition->getName(), $definition);
36
}
37
}
Copied!
Please note that your subscriber has to have a higher priority point to ensure your event is added before any subscriber BusinessEventCollectorEvent to prevent missing awareness or action. I set 1000 for onAddExampleEvent action:
<plugin root>/src/Core/Checkout/Customer/Subscriber/BusinessEventCollectorSubscriber.php
1
public static function getSubscribedEvents()
2
{
3
return [
4
BusinessEventCollectorEvent::NAME => ['onAddExampleEvent', 1000],
5
];
6
}
Copied!
And don't forget to register your subscriber to the container at <plugin root>/src/Resources/config/services.xml
<plugin root>/src/Resources/config/services.xml
1
<service id="Swag\ExamplePlugin\Core\Checkout\Customer\Subscriber\BusinessEventCollectorSubscriber">
2
<argument type="service" id="Shopware\Core\Framework\Event\BusinessEventCollector"/>
3
<tag name="kernel.event_subscriber"/>
4
</service>
Copied!
Well done, you have successfully created your own flow trigger.

Let's check the result

Go to administration page -> Settings -> Flow Builder, then click Add flow to create a new flow, search for Example Event. You could see your event is available and having actions related to the Customer likes to Add tag, Remove tag, etc...
Flow Builder Action Example