custom/plugins/AcrisProductInfoTextCS/src/Subscriber/ProductLoadedSubscriber.php line 46

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\ProductInfoText\Subscriber;
  3. use Acris\ProductInfoText\Components\ProductInfoText\InfoTextGateway;
  4. use Acris\ProductInfoText\Components\ProductInfoText\InfoTextService;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\Struct\ArrayEntity;
  7. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class ProductLoadedSubscriber implements EventSubscriberInterface
  10. {
  11.     public const ACRIS_STREAM_IDS_EXTENSION 'acrisStreamIds';
  12.     /**
  13.      * @var EntityRepositoryInterface
  14.      */
  15.     private $productRepository;
  16.     /**
  17.      * @var InfoTextService
  18.      */
  19.     private $infoTextService;
  20.     /**
  21.      * @var InfoTextGateway
  22.      */
  23.     private $infoTextGateway;
  24.     public function __construct(
  25.         EntityRepositoryInterface $productRepository,
  26.         InfoTextService $infoTextService,
  27.         InfoTextGateway $infoTextGateway
  28.     ) {
  29.         $this->productRepository $productRepository;
  30.         $this->infoTextService $infoTextService;
  31.         $this->infoTextGateway $infoTextGateway;
  32.     }
  33.     public static function getSubscribedEvents()
  34.     {
  35.         return [
  36.             ProductPageLoadedEvent::class => ['productLoaded'200]
  37.         ];
  38.     }
  39.     public function productLoaded(ProductPageLoadedEvent $event): void
  40.     {
  41.         $product $event->getPage()->getProduct();
  42.         if ($product->hasExtension(self::ACRIS_STREAM_IDS_EXTENSION) && !empty($product->getExtension(self::ACRIS_STREAM_IDS_EXTENSION))) {
  43.             $productStreamIds $product->getExtension(self::ACRIS_STREAM_IDS_EXTENSION)->get('ids');
  44.         } else {
  45.             $productStreamIds $this->infoTextGateway->getProductStreamIds([$product->getId()], $event->getSalesChannelContext()->getContext());
  46.             $product->addExtension(self::ACRIS_STREAM_IDS_EXTENSION, new ArrayEntity(['ids' => $productStreamIds]));
  47.         }
  48.         if (empty($productStreamIds)) return;
  49.         $infoTextResult $this->infoTextGateway->getProductDetailInfoText($productStreamIds$event->getSalesChannelContext());
  50.         if(empty($infoTextResult) || $infoTextResult->count() === 0) {
  51.             return;
  52.         }
  53.         $infoTexts $this->infoTextService->getInfoTextsForProduct($infoTextResult->getEntities());
  54.         $product->addExtension('acrisInfoText', new ArrayEntity([
  55.             'texts' => $infoTexts
  56.         ]));
  57.     }
  58. }