<?php declare(strict_types=1);
namespace Swkweb\BuyXPayY\Storefront\Page\Product;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Swkweb\BuyXPayY\Core\Checkout\BuyXPayYPromotion\SalesChannel\BuyXPayYPromotionGateway;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
final class ProductPageLoaderSubscriber implements EventSubscriberInterface
{
/**
* @var BuyXPayYPromotionGateway
*/
private $gateway;
public function __construct(BuyXPayYPromotionGateway $gateway)
{
$this->gateway = $gateway;
}
public static function getSubscribedEvents(): array
{
return [
ProductPageLoadedEvent::class => 'onProductPageLoaded',
];
}
public function onProductPageLoaded(ProductPageLoadedEvent $event): void
{
$productId = $event->getPage()->getProduct()->getId();
$promotions = $this->gateway->getPromotions([$productId], $event->getSalesChannelContext());
$event->getPage()->addExtension('swkwebBuyXPayYPromotion', $promotions->getBestPromotion($productId));
}
}