Create post plugins for Data Exchange API

Edit on GitHub

In some cases, after data is imported or changed through Data Exchange API, you might want particular events to be executed automatically. This document describes how to set up such events by creating custom post plugins.

To set up events that are executed after data import, you need to create a class that implements the Spryker\Zed\DynamicEntityExtension\Dependency\Plugin\DynamicEntityPostCreatePluginInterface interface. For events that are executed after data updates, use the Spryker\Zed\DynamicEntityExtension\Dependency\Plugin\DynamicEntityPostUpdatePluginInterface interface. For more details on these interfaces, see Install the Data Exchange API + Category Management feature or Install the Data Exchange API + Inventory Management feature.

Prerequisites

Create a post plugin to activate products

As an example, this guide explains how to create a plugin that activates products after they’re imported:

  1. Implement a DynamicEntityPostCreatePluginInterface interface with the postCreate(DynamicEntityPostEditRequestTransfer $dynamicEntityPostEditRequestTransfer): DynamicEntityPostEditResponseTransfer method. Example:
<?php

namespace Pyz\Zed\CustomModule\Communication\Plugin\DynamicEntity;

use Generated\Shared\Transfer\DynamicEntityPostEditRequestTransfer;
use Generated\Shared\Transfer\DynamicEntityPostEditResponseTransfer;
use Spryker\Zed\DynamicEntityExtension\Dependency\Plugin\DynamicEntityPostCreatePluginInterface;
use Spryker\Zed\Kernel\Communication\AbstractPlugin;

class CustomDynamicEntityPostCreatePlugin extends AbstractPlugin implements DynamicEntityPostCreatePluginInterface
{
    protected const TABLE_NAME = 'spy_product';

    protected const ID_PRODUCT = 'id_product';

    public function postCreate(DynamicEntityPostEditRequestTransfer $dynamicEntityPostEditRequestTransfer): DynamicEntityPostEditResponseTransfer
    {
        if ($dynamicEntityPostEditRequestTransfer->getTableName() !== static::TABLE_NAME) {
            return new DynamicEntityPostEditResponseTransfer();
        }

        foreach ($dynamicEntityPostEditRequestTransfer->getRawDynamicEntities() as $rawDynamicEntity) {
            $this->productFacade->activateProductConcrete($rawDynamicEntity->getFields()[static::ID_PRODUCT]);
        }

        return new DynamicEntityPostEditResponseTransfer();
    }
}
  1. To register the plugin, add it to the DynamicEntityDependencyProvider in the module.

src/Pyz/Zed/DynamicEntity/DynamicEntityDependencyProvider.php

<?php

namespace Pyz\Zed\DynamicEntity;

use Pyz\Zed\CustomModule\Communication\Plugin\CustomDynamicEntityPostCreatePlugin;
use Spryker\Zed\DynamicEntity\DynamicEntityDependencyProvider as SprykerDynamicEntityDependencyProvider;

class DynamicEntityDependencyProvider extends SprykerDynamicEntityDependencyProvider
{
    /**
     * @return array<\Spryker\Zed\DynamicEntityExtension\Dependency\Plugin\DynamicEntityPostCreatePluginInterface>
     */
    protected function getDynamicEntityPostCreatePlugins(): array
    {
        return [
            new CustomDynamicEntityPostCreatePlugin(),
        ];
    }
}
Verification
  1. Configure the Product entity in spy_dynamic_entity_configuration. For instructions, see Configure Data Exchange API.
  2. Send a POST request to import product data. For instructions, see Sending requests with Data Exchange API.
  3. Send a GET request to check if the product is activated. For instructions, see Sending requests with Data Exchange API.