<?php
declare(strict_types=1);
namespace NetInventors\NetiNextEasyCouponDesigns\Subscriber;
use NetInventors\NetiNextEasyCouponDesigns\Struct\PluginConfigStruct;
use Shopware\Core\Content\Product\ProductEntity;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use NetInventors\NetiNextEasyCouponDesigns\Service\ProductService;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class EntitySubscriber implements EventSubscriberInterface
{
protected PluginConfigStruct $pluginConfig;
protected ProductService $productService;
public function __construct(PluginConfigStruct $pluginConfig, ProductService $productService)
{
$this->pluginConfig = $pluginConfig;
$this->productService = $productService;
}
public static function getSubscribedEvents(): array
{
return [
'product.loaded' => 'onProductLoaded',
];
}
public function onProductLoaded(EntityLoadedEvent $event): void
{
if (!$this->pluginConfig->isActive()) {
return;
}
/** @var ProductEntity $product */
foreach ($event->getEntities() as $product) {
// Yes, this method will be called for every loaded product, but I think it is not a big deal because the logic does not load data from the database or similar
$this->productService->loadAdditionalDesignInformation(
$product,
$event->getContext()
);
}
}
}