custom/plugins/SwkwebBuyXPayY/src/Storefront/Page/Product/ProductPageLoaderSubscriber.php line 28

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Swkweb\BuyXPayY\Storefront\Page\Product;
  3. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  4. use Swkweb\BuyXPayY\Core\Checkout\BuyXPayYPromotion\SalesChannel\BuyXPayYPromotionGateway;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. final class ProductPageLoaderSubscriber implements EventSubscriberInterface
  7. {
  8.     /**
  9.      * @var BuyXPayYPromotionGateway
  10.      */
  11.     private $gateway;
  12.     public function __construct(BuyXPayYPromotionGateway $gateway)
  13.     {
  14.         $this->gateway $gateway;
  15.     }
  16.     public static function getSubscribedEvents(): array
  17.     {
  18.         return [
  19.             ProductPageLoadedEvent::class => 'onProductPageLoaded',
  20.         ];
  21.     }
  22.     public function onProductPageLoaded(ProductPageLoadedEvent $event): void
  23.     {
  24.         $productId $event->getPage()->getProduct()->getId();
  25.         $promotions $this->gateway->getPromotions([$productId], $event->getSalesChannelContext());
  26.         $event->getPage()->addExtension('swkwebBuyXPayYPromotion'$promotions->getBestPromotion($productId));
  27.     }
  28. }