custom/plugins/NetiNextEasyCouponDesigns/src/Subscriber/EntitySubscriber.php line 32

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace NetInventors\NetiNextEasyCouponDesigns\Subscriber;
  4. use Shopware\Core\Content\Product\ProductEntity;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  6. use NetInventors\NetiNextEasyCouponDesigns\Service\PluginConfig;
  7. use NetInventors\NetiNextEasyCouponDesigns\Service\ProductService;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class EntitySubscriber implements EventSubscriberInterface
  10. {
  11.     protected PluginConfig   $pluginConfig;
  12.     protected ProductService $productService;
  13.     public function __construct(PluginConfig $pluginConfigProductService $productService)
  14.     {
  15.         $this->pluginConfig   $pluginConfig;
  16.         $this->productService $productService;
  17.     }
  18.     public static function getSubscribedEvents(): array
  19.     {
  20.         return [
  21.             'product.loaded' => 'onProductLoaded',
  22.         ];
  23.     }
  24.     public function onProductLoaded(EntityLoadedEvent $event): void
  25.     {
  26.         if (!$this->pluginConfig->isActive()) {
  27.             return;
  28.         }
  29.         /** @var ProductEntity $product */
  30.         foreach ($event->getEntities() as $product) {
  31.             // 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
  32.             $this->productService->loadAdditionalDesignInformation(
  33.                 $product,
  34.                 $event->getContext()
  35.             );
  36.         }
  37.     }
  38. }