custom/plugins/ThemeOkeonline/src/Storefront/Page/Product/ProductPageLoadedEventSubscriber.php line 61

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace ThemeOkeOnline\Storefront\Page\Product;
  3. use Shopware\Core\Content\Product\Exception\ReviewNotActiveExeption;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\System\SystemConfig\SystemConfigService;
  8. use Shopware\Storefront\Page\Product\ProductLoaderCriteriaEvent;
  9. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  10. use Shopware\Storefront\Page\Product\Review\ProductReviewLoader;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. use ThemeOkeOnline\Core\Content\Product\ProductSiblingService;
  13. class ProductPageLoadedEventSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var ProductPageLoadedEvent
  17.      */
  18.     private $event;
  19.     private $context;
  20.     private $page;
  21.     private $request;
  22.      /**
  23.      * @var ProductSiblingService
  24.      */
  25.     private $productSiblingService;
  26.     /**
  27.      * @var ProductReviewLoader
  28.      */
  29.     private $productReviewLoader;
  30.     /**
  31.      * @var SystemConfigService
  32.      */
  33.     private $systemConfigService;
  34.     public function __construct(
  35.         ProductSiblingService $productSiblingService,
  36.         ProductReviewLoader $productReviewLoader,
  37.         SystemConfigService $systemConfigService
  38.     )
  39.     {
  40.         $this->productSiblingService $productSiblingService;
  41.         $this->productReviewLoader $productReviewLoader;
  42.         $this->systemConfigService $systemConfigService;
  43.     }
  44.     public static function getSubscribedEvents(): array
  45.     {
  46.         return [
  47.             'Shopware\Storefront\Page\Product\ProductPageLoadedEvent' => ['productLoaded']
  48.         ];
  49.     }
  50.     public function productLoaded(ProductPageLoadedEvent $event): void
  51.     {
  52.         $this->addReviewDataToProductPage($event);
  53.         $this->addSiblingDataToProductPage($event);
  54.     }
  55.     public function addSiblingDataToProductPage(ProductPageLoadedEvent $event): void
  56.     {
  57.         $this->event $event;
  58.         $context $event->getContext();
  59.         $page $event->getPage();
  60.         $product $page->getProduct();
  61.         $configuratorSettings $page->getConfiguratorSettings();
  62.         $data $this->productSiblingService->getSiblingsForProduct($product$context);
  63.         $page->addExtension('siblings'$data);
  64.     }
  65.     /*
  66.      * Somehow the reviews are not available when using our plugin, but we will get them back!
  67.      */
  68.     public function addReviewDataToProductPage($event): void
  69.     {
  70.         $this->event $event;
  71.         $this->context $event->getSalesChannelContext();
  72.         $this->page $event->getPage();
  73.         $this->request $event->getRequest();
  74.         $this->checkReviewsActive();
  75.         $reviews $this->getProductReviews();
  76.         $this->page->addExtension('reviews'$reviews);
  77.     }
  78.     private function getProductReviews(): Object
  79.     {
  80.         $product $this->page->getProduct();
  81.         $reviews $this->productReviewLoader->load($this->request$this->context);
  82.         $reviews->setParentId($product->getParentId() ?? $product->getId());
  83.         return $reviews;
  84.     }
  85.     /**
  86.      * @throws ReviewNotActiveExeption
  87.      */
  88.     private function checkReviewsActive(): void
  89.     {
  90.         $showReview $this->systemConfigService->get('core.listing.showReview'$this->context->getSalesChannel()->getId());
  91.         if (!$showReview) {
  92.             throw new ReviewNotActiveExeption();
  93.         }
  94.     }
  95. }