<?php declare(strict_types=1);
namespace Acris\ProductDownloads\Storefront\Subscriber;
use Acris\ProductDownloads\Components\ProductDownloadService;
use Acris\ProductDownloads\Custom\ProductDownloadCollection;
use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class ProductPageSubscriber implements EventSubscriberInterface
{
private ProductDownloadService $productDownloadService;
public function __construct(ProductDownloadService $productDownloadService)
{
$this->productDownloadService = $productDownloadService;
}
public static function getSubscribedEvents(): array
{
return [
ProductPageCriteriaEvent::class => 'onProductPageCriteriaLoaded',
ProductPageLoadedEvent::class => 'onProductPageLoaded'
];
}
public function onProductPageCriteriaLoaded(ProductPageCriteriaEvent $event): void
{
$this->productDownloadService->addProductAssociationCriteria($event->getCriteria());
$this->productDownloadService->addProductLinkAssociationCriteria($event->getCriteria());
}
public function onProductPageLoaded(ProductPageLoadedEvent $event):void
{
$languageId = $event->getContext()->getLanguageId();
if($event->getPage() && $event->getPage()->getProduct()) {
$this->productDownloadService->checkLanguageForProduct($event->getPage()->getProduct(), $languageId, $event->getSalesChannelContext());
$this->productDownloadService->checkLanguageForProductLinks($event->getPage()->getProduct(), $languageId, $event->getSalesChannelContext());
}
}
}