custom/plugins/ZweiPunktSeoKampfpreise/src/Subscriber/DetailSubscriber.php line 54

Open in your IDE?
  1. <?php
  2. namespace ZweiPunktSeoKampfpreise\Subscriber;
  3. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  4. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  7. use ZweiPunktSeoKampfpreise\Service\ConfigService;
  8. use ZweiPunktSeoKampfpreise\Service\ProductPriceUpdater;
  9. use ZweiPunktSeoKampfpreise\Service\RequestChecker;
  10. /**
  11.  * This subscriber updated the product price if the fightprice parameter is
  12.  * set correctly and adds the product ID to the session so the product price
  13.  * will be updated in feature requests even if the fightprice parameter isn't
  14.  * set at the second visit.
  15.  */
  16. class DetailSubscriber implements EventSubscriberInterface
  17. {
  18.     private SessionInterface $session;
  19.     private SalesChannelProductEntity $product;
  20.     private ConfigService $configService;
  21.     private RequestChecker $requestChecker;
  22.     private ProductPriceUpdater $productPriceUpdater;
  23.     public function __construct(
  24.         ConfigService $configService,
  25.         RequestChecker $requestChecker,
  26.         ProductPriceUpdater $productPriceUpdater
  27.     ) {
  28.         $this->configService $configService;
  29.         $this->requestChecker $requestChecker;
  30.         $this->productPriceUpdater $productPriceUpdater;
  31.     }
  32.     /**
  33.      * @return array<string, string>
  34.      */
  35.     public static function getSubscribedEvents()
  36.     {
  37.         return [
  38.             ProductPageLoadedEvent::class => 'productPageLoaded',
  39.         ];
  40.     }
  41.     /**
  42.      * @param ProductPageLoadedEvent $event
  43.      */
  44.     public function productPageLoaded(
  45.         ProductPageLoadedEvent $event
  46.     ): void {
  47.         $this->configService->setSalesChannelId(
  48.             $event->getSalesChannelContext()->getSalesChannelId()
  49.         );
  50.         $this->requestChecker->setRequest($event->getRequest());
  51.         $this->session $event->getRequest()->getSession();
  52.         if (!$this->requestChecker->updatePrice()) {
  53.             return;
  54.         }
  55.         $this->product $event->getPage()->getProduct();
  56.         $ruleId $this->configService->get('fightPriceRule');
  57.         $prices $this->product->getPrices();
  58.         $fightPrice null;
  59.         foreach ($prices as $productPriceEntity) {
  60.             if ($ruleId == $productPriceEntity->getRuleId()) {
  61.                 $fightPrice $productPriceEntity;
  62.             }
  63.         }
  64.         if (is_null($fightPrice)) {
  65.             return;
  66.         }
  67.         if (!$this->requestChecker->isSeoPage()) {
  68.             return;
  69.         }
  70.         $this
  71.             ->productPriceUpdater
  72.             ->updatePrice($ruleId$this->product)
  73.         ;
  74.         $this->addFightPriceToSession(
  75.             $fightPrice->getPrice()->first()->getGross()
  76.         );
  77.     }
  78.     private function addFightPriceToSession(float $price): void
  79.     {
  80.         $history $this->session->get('priceHistory', []);
  81.         $history[$this->product->getId()] = round($price2);
  82.         $this->session->set('priceHistory'$history);
  83.     }
  84. }