custom/plugins/MoorlProductAccessories/src/Subscriber/StorefrontSubscriber.php line 52

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace MoorlProductAccessories\Subscriber;
  3. use MoorlProductAccessories\Core\Service\ProductAccessoriesService;
  4. use Shopware\Core\System\SalesChannel\Entity\SalesChannelEntityLoadedEvent;
  5. use Shopware\Core\System\SystemConfig\SystemConfigService;
  6. use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
  7. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class StorefrontSubscriber implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var SystemConfigService
  13.      */
  14.     private $systemConfigService;
  15.     /**
  16.      * @var ProductAccessoriesService
  17.      */
  18.     private $productAccessoriesService;
  19.     public function __construct(
  20.         SystemConfigService $systemConfigService,
  21.         ProductAccessoriesService $productAccessoriesService
  22.     )
  23.     {
  24.         $this->systemConfigService $systemConfigService;
  25.         $this->productAccessoriesService $productAccessoriesService;
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             'sales_channel.moorl_pa_accessory.loaded' => 'loaded',
  31.             ProductPageCriteriaEvent::class => 'addProductPageCriteria',
  32.             ProductPageLoadedEvent::class => 'enrichProduct'
  33.         ];
  34.     }
  35.     public function enrichProduct(ProductPageLoadedEvent $event): void
  36.     {
  37.         $this->productAccessoriesService->setSalesChannelContext($event->getSalesChannelContext());
  38.         $this->productAccessoriesService->enrichProductEntity($event->getPage()->getProduct());
  39.     }
  40.     public function addProductPageCriteria(ProductPageCriteriaEvent $event): void
  41.     {
  42.         $this->productAccessoriesService->enrichProductCriteria($event->getCriteria());
  43.     }
  44.     public function loaded(SalesChannelEntityLoadedEvent $event): void
  45.     {
  46.         $this->productAccessoriesService->setSalesChannelContext($event->getSalesChannelContext());
  47.         foreach ($event->getEntities() as $accessory) {
  48.             $this->productAccessoriesService->handlePrice($accessory);
  49.         }
  50.     }
  51. }