Event Procedures

Event procedures are triggered on certain events if the given filters apply. This tutorial will show you how to add custom event procedures and filters with a plugin. To achieve this, excerpts of a small plugin with the name ProcedurePlugin will be shown and explained.

Procedures

This chapter will show you how to add a custom procedure. A procedure is the part of an event procedure that will be executed if filters apply. The example procedure here will set the status of an order to 3.

Registering the procedure

The procedure has to be registered in the boot method of a service provider of the plugin by using EventProceduresService.

ProcedurePlugin\Providers\ProcedurePluginServiceProvider
<?php
namespace ProcedurePlugin\Providers;

use Plenty\Plugin\ServiceProvider;
use Plenty\Modules\EventProcedures\Services\EventProceduresService;
use Plenty\Modules\EventProcedures\Services\Entries\ProcedureEntry;
use ProcedurePlugin\EventProcedures\Procedures;

class ProcedurePluginServiceProvider extends ServiceProvider
{
    /**
     * @param EventProceduresService $eventProceduresService
     * @return void
     */
    public function boot(EventProceduresService $eventProceduresService)
    {
        $eventProceduresService->registerProcedure(
            'setStatus',
            ProcedureEntry::EVENT_TYPE_ORDER,
            ['de' => 'Setze Status auf 3', 'en' => 'Set status to 3'],
            Procedures::class . '@setStatus'
        );
    }
}

Defining the action

Define the procedures in the class that you specified at the registration.

ProcedurePlugin\EventProcedures\Procedures
<?php
namespace ProcedurePlugin\EventProcedures;

use Plenty\Modules\EventProcedures\Events\EventProceduresTriggered;
use Plenty\Modules\Order\Contracts\OrderRepositoryContract;

class Procedures
{
    /**
     * @param EventProceduresTriggered $event
     * @return void
     */
    public function setStatus(EventProceduresTriggered $event)
    {
        $order = $event->getOrder();
        $orderRepository = pluginApp(OrderRepositoryContract::class);
        $orderRepository->updateOrder(['statusId' => 3], $order->id);
    }
}

Filters

This chapter will show you how to add a custom filter. You can use filters to define under which circumstances an event procedure will be executed. The filter in our example will check if the order is locked.

Registering the filter

The filter has to be registered in the boot method of a service provider of the plugin by using EventProceduresService.

ProcedurePlugin\Providers\ProcedurePluginServiceProvider
<?php
namespace ProcedurePlugin\Providers;

use Plenty\Plugin\ServiceProvider;
use Plenty\Modules\EventProcedures\Services\EventProceduresService;
use Plenty\Modules\EventProcedures\Services\Entries\ProcedureEntry;
use ProcedurePlugin\EventProcedures\Filters;

class ProcedurePluginServiceProvider extends ServiceProvider
{
    /**
     * @param EventProceduresService $eventProceduresService
     * @return void
     */
    public function boot(EventProceduresService $eventProceduresService)
    {
        $eventProceduresService->registerFilter(
            'orderLocked',
            ProcedureEntry::EVENT_TYPE_ORDER,
            ['de' => 'Auftrag ist gesperrt', 'en' => 'Order is locked'],
            Filters::class . '@orderLocked'
        );
    }
}

Defining the filter logic

Define the filter logic in the class that you specified at the registration.

ProcedurePlugin\EventProcedures\Filters
<?php
namespace ProcedurePlugin\EventProcedures;

use Plenty\Modules\EventProcedures\Events\EventProceduresTriggered;

class Filters
{
    /**
     * @param EventProceduresTriggered $event
     * @return boolean
     */
    public function orderLocked(EventProceduresTriggered $event)
    {
        return $event->getOrder()->lockStatus != 'unlocked';
    }
}